Exemplo n.º 1
0
        public void ShouldGetExpectedMessageWhenBroadcastTopicGivenMutilConsume()
        {
            var consume  = new DataConsumer();
            var consume2 = new DataConsumer();
            var consume3 = new DataConsumer();

            using (var host = new KnifeHost(new string[0], (build, sc) =>
            {
                sc.AddSingleton <IEventConsumer>(consume);
                sc.AddSingleton <IEventConsumer>(consume2);
                sc.AddSingleton <IEventConsumer>(consume3);
            }))
            {
                var appLiftTime = host.GetService <IHostApplicationLifetime>();
                var producer    = host.GetService <IEventProducer>();
                Task.WaitAll(
                    Task.Run(host.Run),
                    // wait 800ms for the consumes ready.
                    Task.Delay(800).ContinueWith(_ => BroadcastTopicData(producer, 100)),
                    Task.Delay(2000).ContinueWith(_ => appLiftTime.StopApplication())
                    );
            }
            Assert.AreEqual(100, consume.Received.Count);
            Assert.AreEqual(100, consume2.Received.Count);
            Assert.AreEqual(100, consume3.Received.Count);
        }
Exemplo n.º 2
0
        public void ShouldGetExpectedMessageWhenEnqueueGivenMutilConsume()
        {
            var consume1 = new DataConsumer();
            var consume2 = new DataConsumer();
            var consume3 = new DataConsumer();

            using (var host = new KnifeHost(new string[0], (build, sc) =>
            {
                sc.AddSingleton <IEventConsumer>(consume1);
                sc.AddSingleton <IEventConsumer>(consume2);
                sc.AddSingleton <IEventConsumer>(consume3);
            }))
            {
                var appLiftTime = host.GetService <IHostApplicationLifetime>();
                var producer    = host.GetService <IEventProducer>();
                EnqueueData(producer, 100);
                Task.WaitAll(
                    Task.Run(host.Run),
                    // wait 1500 ms to consume message
                    Task.Delay(1500).ContinueWith(_ => appLiftTime.StopApplication())
                    );
            }
            var consumeCount = consume1.Received.Count + consume2.Received.Count + consume3.Received.Count;

            Assert.AreEqual(100, consumeCount);
            var allReceived = consume1.Received.Concat(consume2.Received).Concat(consume3.Received);
            var countByKey  = allReceived.Select(p => p.IntProp).Distinct().Count();

            Assert.AreEqual(100, countByKey);
        }
Exemplo n.º 3
0
        public void ShouldGetZeroMessageWhenBroadcastTopicBeforeConsume()
        {
            var consume = new DataConsumer();

            using (var host = new KnifeHost(new string[0], (build, sc) =>
            {
                sc.AddSingleton <IEventConsumer>(consume);
            }))
            {
                var appLiftTime = host.GetService <IHostApplicationLifetime>();
                var producer    = host.GetService <IEventProducer>();
                BroadcastTopicData(producer, 100);
                Task.WaitAll(
                    Task.Run(host.Run),
                    Task.Delay(2000).ContinueWith(_ => appLiftTime.StopApplication())
                    );
            }
            Assert.AreEqual(0, consume.Received.Count);
        }
Exemplo n.º 4
0
 public void ShouldGetInstanceIfNoFilters()
 {
     using (var host = new KnifeHost(
                new Dictionary <string, object>
     {
     }))
     {
         var instance = host.GetService <MyService1>();
         Assert.IsNotNull(instance);
     }
 }
Exemplo n.º 5
0
 public void ShouldNotGetInstanceWhenFilterTypeByWildcard()
 {
     using (var host = new KnifeHost(
                new Dictionary <string, object>
     {
         ["Knife:Ignores:Types:0"] = "*.MyService?"
     }))
     {
         var instance = host.GetService <MyService1>();
         Assert.IsNull(instance);
     }
 }
Exemplo n.º 6
0
 public void ShouldNotGetInstanceWhenFilterTypeByName()
 {
     using (var host = new KnifeHost(
                new Dictionary <string, object>
     {
         ["Knife:Ignores:Types:0"] = typeof(MyService1).FullName
     }))
     {
         var instance = host.GetService <MyService1>();
         Assert.IsNull(instance);
     }
 }
Exemplo n.º 7
0
 public void ShouldNotGetInstanceWhenFilterAssemblyByName()
 {
     using (var host = new KnifeHost(
                new Dictionary <string, object>
     {
         ["Knife:Ignores:Assemblies:0"] = this.GetType().Assembly.GetName().Name
     }))
     {
         var instance = host.GetService <MyService1>();
         Assert.IsNull(instance);
     }
 }
Exemplo n.º 8
0
 public void ShouldNotGetInstanceWhenFilterAssemblyByWildcard()
 {
     using (var host = new KnifeHost(
                new Dictionary <string, object>
     {
         ["Knife:Ignores:Assemblies:0"] = "*"
     }))
     {
         var instance = host.GetService <MyService1>();
         instance.Should().NotBeNull();
     }
 }
Exemplo n.º 9
0
        public void ShouldNotBeNullWhenHasDefineMySqlDbContextClassAndWithInjectType()
        {
            var args = new Dictionary <string, object>
            {
                ["ConnectionStrings:TestContext2"] = "Server=localhost;Database=SequenceContext; User=root;Password=;"
            };

            using (var host = new KnifeHost(args))
            {
                Assert.IsNotNull(host.GetService <TestDbContext2>());
            }
        }