Example #1
0
        private string ApplySingle(RedisMutex mutex, string codeIdentifier, string requestIdentifier)
        {
            var values = new Dictionary <string, string>
            {
                { "item_id", "191" },
                { "itemDiscount_ids[]", "216" },
                { "coupon_code", "TEST" },
                { "flowContext[codeIdentifier]", codeIdentifier },
                { "flowContext[requestIdentifier]", requestIdentifier }
            };

            var content = new FormUrlEncodedContent(values);

            try
            {
                client.DefaultRequestHeaders.Add("Connection", "close");
                var response        = client.PostAsync(apiServerUri + "/discount/apply", content).Result;
                var responseContent = response.Content;
                var responseString  = responseContent.ReadAsStringAsync().Result;
                response.Dispose();
                return(responseString);
            }
            catch (Exception)
            {
            }

            return(null);
        }
Example #2
0
        private RedisMutex BuildMutex(string codeIdentifier, string requestIdentifier)
        {
            var        mutexIdentifier = "mutex:" + codeIdentifier + ":" + requestIdentifier;
            RedisMutex mutex           = new RedisMutex(redisServerUri, mutexIdentifier);
            bool       result          = mutex.Acquire(100000);

            return(mutex);
        }
        public void AcquireTest()
        {
            RedisMutex mutex  = new RedisMutex("192.168.56.3", ":)");
            bool       result = mutex.Acquire(10000);

            Assert.IsTrue(result);
            result = mutex.Acquire(10000);
            Assert.IsFalse(result);
            mutex.Release();
            result = mutex.Acquire(10000);
            Assert.IsTrue(result);
        }
Example #4
0
        public void TestMethod()
        {
            string codeIdentifier     = "f6cf5d7322b3493f99a7fb6bb5cf1924";
            string requestIdentifier1 = "2d92b2d3f0a44ef996130a27dc982866";
            string requestIdentifier2 = "2d92b2d3f0a44ef996130a27dc982867";

            RedisMutex mutex1 = this.BuildMutex(codeIdentifier, requestIdentifier1);
            RedisMutex mutex2 = this.BuildMutex(codeIdentifier, requestIdentifier2);

            TimeSpan alertTime = TimeSpan.FromSeconds(5);

            ExecuteDelayed(alertTime, () =>
            {
                mutex1.Release();
                mutex2.Release();
            });

            string response1 = this.ApplySingle(mutex1, codeIdentifier, requestIdentifier1);
            string response2 = this.ApplySingle(mutex2, codeIdentifier, requestIdentifier2);

            Assert.AreEqual(response1, "{\"result\":true}");
            Assert.AreEqual(response2, "{\"result\":true}");
        }
Example #5
0
 public TestContext(RedisMutex mutex, string response)
 {
     this.Mutex    = mutex;
     this.Response = response;
 }