Пример #1
0
        public void AddingItemToFullQueueKeepsSameSize()
        {
            // Arrange
            var queue = new LimitQueue(limit: limit);

            queue.Enqueue(1);
            queue.Enqueue(2);
            queue.Enqueue(3);

            // Act
            queue.Enqueue(4);

            // Assert
            Assert.AreEqual(queue.Count, limit);
        }
Пример #2
0
        public void AddingItemToFullQueueGetsRidOfItemFromFrontOfQueue()
        {
            // Arrange
            var toBeDropped = 1;
            var toBeFront   = 2;
            var queue       = new LimitQueue(limit: limit);

            queue.Enqueue(toBeDropped);
            queue.Enqueue(toBeFront);
            queue.Enqueue(3);

            // Act
            queue.Enqueue(4);
            var front = (int)queue.Dequeue();

            // Assert
            Assert.AreEqual(front, toBeFront);
            Assert.AreNotEqual(front, toBeDropped);
            Assert.AreEqual(queue.Count, limit - 1);
        }
        public void Execute(ReportRow row, Action <string, object[]> log = null)
        {
            if (row["AccessToken"] == lastAccessToken && errorCodeQue.IsFull() && errorCodeQue.Distinct().Count() == 1)
            {
                //重复5次以上错误,略过处理
                return;
            }

            /*
             * <?xml version="1.0" encoding="utf-8"?>
             * <Response>
             * <Code>2000</Code>
             * <Message>Merchant unable to access API due to account's status.</Message>
             * <Data></Data>
             * </Response>
             */

            string apiFormat = "https://china-merchant.wish.com/api/v2/product/update-multi-shipping";
            string apiRequestUrl = string.Format("access_token={0}&format=xml&id={1}", row["AccessToken"], row["WishId"]);
            string responseText = string.Empty;
            string responseCode = "0", errorFormat = "#WishId:{0}, Error:{1}";

            string appendParam = AppendProductShipFeeParameters(Convert.ToDouble(row["SoldPrice"]), Convert.ToDouble(row["ShippingFee"]));

            if (appendParam.Length > 3)
            {
                apiRequestUrl += appendParam;
            }
            else
            {
                return;
            }

InvokeStart:
            try
            {
                //responseText = Encoding.UTF8.GetString(NC.UploadData(apiFormat, Encoding.UTF8.GetBytes(apiRequestUrl)));
                responseText = GetCurrentResult()(apiFormat + "?" + apiRequestUrl);
                responseCode = TageInnerText(responseText, "Code");
                if (responseCode != "0")
                {
                    if (log != null)
                    {
                        log(errorFormat, new object[] { row["WishId"], responseText });
                    }
                    else
                    {
                        Console.WriteLine(string.Format(errorFormat, row["WishId"], responseText));
                    }
                }
                else
                {
                    retryTimes = 0;
                }
            }
            catch (WebException wEx)
            {
                if (wEx.Response != null)
                {
                    using (StreamReader sr = new StreamReader(wEx.Response.GetResponseStream()))
                    {
                        responseText = sr.ReadToEnd();
                        sr.Close();
                    }
                }

                responseText = string.IsNullOrEmpty(responseText) ? wEx.Message : responseText;
                responseCode = TageInnerText(responseText, "Code");
                if (string.IsNullOrEmpty(responseCode))
                {
                    //没有错误的Code
                    retryTimes++;
                    if (retryTimes < 3)
                    {
                        System.Threading.Thread.Sleep(1000 * 3);
                        goto InvokeStart;
                    }
                }
                else
                {
                    errorCodeQue.Enqueue(responseCode);
                    string errMsg = TageInnerText(responseText, "Message");
                    if (log != null)
                    {
                        log("URL:{0}, Error:{1}", new object[] { apiRequestUrl, errMsg });
                    }
                    else
                    {
                        Console.WriteLine(string.Format("URL:{0}, Error:{1}", apiRequestUrl, errMsg));
                    }
                }
            }
            catch (Exception ncEx)
            {
                if (log != null)
                {
                    log("URL:{0}, Error:{1}", new object[] { apiRequestUrl, ncEx.Message });
                }

                retryTimes++;
                if (retryTimes < 3)
                {
                    System.Threading.Thread.Sleep(1000 * 3);
                    goto InvokeStart;
                }
            }
            finally
            {
                lastAccessToken = row["AccessToken"];
            }
        }