Пример #1
0
 public void JoinTest()
 {
     var data = new List<String>() { "A", "B", "C", "D", "E" };
       var expected = "A, B, C, D, E";
       var actual = data.Join(", ");
       Assert.AreEqual(expected, actual);
 }
        public void Join()
        {
            var emptyList = new List<string>();
            var oneItemList = new List<string> { "One" };
            var twoItemList = new List<string> { "One", "Two" };

            Assert.That(emptyList.Join(","), Is.EqualTo(String.Empty));
            Assert.That(oneItemList.Join(","), Is.EqualTo("One"));
            Assert.That(twoItemList.Join(","), Is.EqualTo("One,Two"));
        }
 public void join()
 {
     var list = new List<string>(new[] {"a", "b", "c"});
     list.Join(", ").ShouldEqual("a, b, c");
 }
 public void Join_IgnoresNullElements()
 {
     var listWithNull = new List<string> { "One", null, "Two" };
     Assert.That(listWithNull.Join(","), Is.EqualTo("One,Two"));
 }
Пример #5
0
 public void LIVE_STRESS_TEST_multiple_producers_multiple_queues_one_consumer_per_queue()
 {
     var messagesPerProducer = 100;
     var producerCount = 1;
     var client = CreateLiveClient();
     var queues = new List<string>();
     foreach(var x in new[] { "a", "b", "c", "d" }) {
         var queue = "test-" + x + "-" + StringUtil.CreateAlphaNumericKey(4);
         client.CreateQueue(queue, new Result<AwsSqsResponse>()).Wait();
         queues.Add(queue);
     }
     try {
         var producers = new List<Result<List<string>>>();
         for(var i = 0; i < producerCount; i++) {
             var producer = i;
             producers.Add(Async.Fork(() => {
                 _log.DebugFormat("producer {0} started", producer);
                 var c = CreateLiveClient();
                 var msgs = new List<string>();
                 for(var j = 0; j < messagesPerProducer; j++) {
                     var msg = StringUtil.CreateAlphaNumericKey(1024);
                     foreach(var queue in queues) {
                         c.Send(queue, AwsSqsMessage.FromBody(msg), new Result<AwsSqsSendResponse>()).Wait();
                     }
                     msgs.Add(msg);
                     if(msgs.Count % 10 == 0) {
                         _log.DebugFormat("producer {0} sent {1}/{2} msgs", producer, msgs.Count, messagesPerProducer);
                     }
                 }
                 _log.DebugFormat("producer {0} finished", producer);
                 return msgs;
             }, new Result<List<string>>()));
         }
         var consumers = queues.ToDictionary(queue => queue, queue => Async.Fork(() => {
             _log.DebugFormat("consumer {0} started", queue);
             var c = CreateLiveClient();
             var msgs = new List<string>();
             var expected = messagesPerProducer * producerCount;
             var lastReport = 0;
             while(msgs.Count < expected) {
                 var received = c.ReceiveMax(queue, new Result<IEnumerable<AwsSqsMessage>>()).Wait();
                 var count = 0;
                 foreach(var msg in received) {
                     count++;
                     msgs.Add(msg.Body);
                     c.Delete(msg, new Result<AwsSqsResponse>()).Wait();
                 }
                 if(count > 0 && msgs.Count > lastReport + 10) {
                     _log.DebugFormat("consumer '{0}' received: {1}/{2}", queue, msgs.Count, expected);
                     lastReport = msgs.Count;
                 }
             }
             return msgs;
         }, new Result<List<string>>()));
         producers.Join(new Result()).Wait();
         consumers.Values.Join(new Result()).Wait();
         var allMessages = producers.SelectMany(x => x.Value).OrderBy(x => x).ToArray();
         foreach(var consumed in consumers) {
             var queue = consumed.Key;
             var messages = consumed.Value.Value.OrderBy(x => x).ToArray();
             Assert.AreEqual(allMessages, messages, string.Format("message list for queue '{0}' is wrong", queue));
         }
     } finally {
         foreach(var queue in queues) {
             _log.DebugFormat("cleaning up queue '{0}'", queue);
             client.DeleteQueue(queue, new Result<AwsSqsResponse>()).Wait();
         }
     }
 }
Пример #6
0
 public Result<Production> Produce(int messages)
 {
     var production = new Production();
     var final = new Result<Production>();
     Async.Fork(() => {
         try {
             _log.DebugFormat("{0}: Producing {1} messages", production.Id, messages);
             var responses = new List<Result<string>>();
             var client = new AwsSqsClient(_clientConfig);
             for(var i = 0; i < messages; i++) {
                 var result = new Result<string>();
                 responses.Add(result);
                 var msg = production.Id + ":" + messages;
                 client.Send(_queue, AwsSqsMessage.FromBody(msg), new Result<AwsSqsSendResponse>()).WhenDone(r => {
                     if(r.HasException) {
                         result.Throw(r.Exception);
                         return;
                     }
                     result.Return(msg);
                 });
             }
             responses.Join(new Result()).WhenDone(r => {
                 if(r.HasException) {
                     final.Throw(r.Exception);
                     return;
                 }
                 production.Stopwatch.Stop();
                 production.Sent.AddRange(responses.Select(x => x.Value));
                 _log.DebugFormat("{0}: Sent {1} messages in {2:0.00}s @ {3:0.00}msg/sec",
                     production.Id,
                     production.Sent.Count,
                     production.Stopwatch.Elapsed.TotalSeconds,
                     production.Sent.Count / production.Stopwatch.Elapsed.TotalSeconds
                 );
                 final.Return(production);
             });
         } catch(Exception e) {
             final.Throw(e);
         }
     });
     return final;
 }
Пример #7
0
 public void LIVE_STRESSTEST_Produce_Consume()
 {
     var client = CreateLiveClient();
     var queue = "test-" + StringUtil.CreateAlphaNumericKey(8);
     client.CreateQueue(queue, new Result<AwsSqsResponse>()).Wait();
     try {
         var n = 100;
         var productions = new List<Result<Production>>();
         var producer = new Producer(queue, GetConfig());
         for(var i = 0; i < 4; i++) {
             productions.Add(producer.Produce(n));
         }
         productions.Join(new Result()).Wait();
         var totalMessages = productions.Count * n;
         var consumers = new List<Consumer>();
         for(var i = 0; i < 10; i++) {
             var consumer = new Consumer(queue, CreateLiveClient());
             consumer.Consume();
             consumers.Add(consumer);
         }
         Assert.IsTrue(
             Wait.For(() => consumers.Sum(x => x.Received) == totalMessages, (totalMessages * 0.2).Seconds()),
             string.Format("got {0} instead of the expected {1} messages", consumers.Sum(x => x.Received), totalMessages)
         );
     } finally {
         _log.DebugFormat("cleaning up queue '{0}'", queue);
         client.DeleteQueue(queue, new Result<AwsSqsResponse>()).Wait();
     }
 }
 public void WaitForEmailsToBeSent(List<string> recipients, int waitFor = 5)
 {
     WaitForEmailsWithCondition(waitFor,
                                messages => recipients.Join(messages, x => x, y => y.To.First().Address, (x, y) => x)
                                .Count() == recipients.Count());
 }
Пример #9
0
        private void CodegenAllServicesTest(string language)
        {
            // Discover discovery.
            IService discovery = DiscoverDiscovery();

            // Generate discovery.
            Assembly discoveryAssembly = CodegenDiscovery(discovery);

            // Call the .List method.
            API[] apis = DiscoveryListServices(discoveryAssembly);

            // Run the generator.
            List<Exception> exceptions = new List<Exception>();
            List<string> failedAPIs = new List<string>();
            foreach (API api in apis)
            {
                try
                {
                    Assert.NotNull(TestService(language, api));
                }
                catch (Exception e)
                {
                    // Catch all exceptions, and throw them together.
                    failedAPIs.Add(api.Name);
                    exceptions.Add(e);
                }
            }

            // Create the error message.
            if (exceptions.Count > 0)
            {
                var message = new StringBuilder();
                message.AppendLine(
                    string.Format("Failed to generate {0} out of {1} APIs:", failedAPIs.Count, apis.Length));
                message.AppendLine(String.Join(", ", failedAPIs.ToArray()));
                message.AppendLine();

                int i = 0;
                int j = 0;
                var msgs = failedAPIs.Join(
                    exceptions, str => i++, ex => j++, (str, ex) => new { API = str, Exception = ex });
                foreach (var entries in msgs)
                {
                    message.AppendLine(string.Format("API '{0}' failed with:", entries.API));
                    message.AppendLine(string.Format("   {0}", entries.Exception));
                }

                Assert.Fail(message.ToString());
            }
        }
Пример #10
0
        public void JoinWithComparerQueryReuse()
        {
            List<int> first = new List<int> { 1, 2 };
            List<int> second = new List<int> { 4, 5 };
            IEnumerable<string> enumerable = first.Join(second, x => x % 2 == 0, x => x % 2 == 0, (x, y) => String.Format("{0}:{1}", x, y), EqualityComparer<bool>.Default);

            enumerable.AssertEqual("1:5", "2:4");

            first.Add(3);
            second.Add(6);
            enumerable.AssertEqual("1:5", "2:4", "2:6", "3:5");
        }
Пример #11
0
 public void Join()
 {
     var list = new List<string>() { "first", "second", "third" };
     var joined = list.Join(",");
     Assert.That(joined, Has.Length.EqualTo(18), "Incorrect length.");
 }