Ejemplo n.º 1
0
        public async Task SendReceiveFlow()
        {
            var config = CreateConfig();
            var logger = new Mock <ILogger <BroadcastMessenger> >().Object;

            using var client = new BroadcastMessenger(logger, config);
            using var cts    = new CancellationTokenSource();
            var token = cts.Token;
            var msg   = "test";
            var cnt   = 0;

            client.AnnouncingAsync(msg, token: token);

            client.ListeningAsync((ip, body) =>
            {
                if (msg.Equals(body))
                {
                    if (cnt == 3)
                    {
                        cts.Cancel();
                    }
                    else
                    {
                        cnt++;
                    }
                }
            }, token: token);

            try
            {
                await Task.Delay(TimeSpan.FromSeconds(5), token);
            }
            catch (Exception)
            {
                // ignored
            }

            cts.Cancel();
            Assert.AreEqual(3, cnt);
        }
Ejemplo n.º 2
0
        public void TestBasics()
        {
            const string MODULE_NAME  = "module";
            const int    MODULE_COUNT = 10;
            const int    EP_COUNT     = 10;
            const string MSG          = "MESSAGE";

            ModuleManager   manager       = new();
            MockBaseModule  baseModule    = new();
            MessagingModule messageModule = new();

            List <MockModule> handlerModules = new();

            int i;

            for (i = 0; i < MODULE_COUNT; i++)
            {
                handlerModules.Add(new(MODULE_NAME + i));
            }


            baseModule.MockProtocol.DelayTime = 0;
            for (i = 0; i < MODULE_COUNT / 2; i++)
            {
                TestUtils.AssertTask(manager.RegisterModuleAsync(handlerModules[i].Header));
            }

            TestUtils.AssertTask(manager.RegisterModuleAsync(baseModule.Header));
            TestUtils.AssertTask(manager.RegisterModuleAsync(messageModule.Header));

            for (; i < MODULE_COUNT; i++)
            {
                TestUtils.AssertTask(manager.RegisterModuleAsync(handlerModules[i].Header));
            }


            BroadcastMessenger messenger = TestUtils.AssertTask(manager.GetInterfaceAsync <BroadcastMessenger>(null));

            ConcurrentBag <MockEndpoint> endpoints = new();

            TestUtils.AssertTask(AsyncUtilities.RepeatAsync(() =>
            {
                var ep = new MockEndpoint();
                baseModule.ConnectEndpoint(ep);
                endpoints.Add(ep);
                return(Task.CompletedTask);
            }, EP_COUNT));

            ConcurrentDictionary <Tuple <MockMessageHandler, MockEndpoint>, Message> msgDic = new();

            baseModule.MockProtocol.MessageErrorHandler = async error =>
            {
                Assert.IsFalse(true, "should never be reached!");
            };


            AsyncBarrier barrier = new(EP_COUNT * MODULE_COUNT, false);

            AsyncInt c = 0;

            //send a message to each handler via each endpoint
            foreach (var module in handlerModules)
            {
                module.Handler.MessageHandlerCallback = async(handler, ep, msg) =>
                {
                    Message expectedMsg;
                    Assert.IsTrue(msgDic.TryGetValue((handler, (MockEndpoint)ep).ToTuple(), out expectedMsg));
                    Assert.AreEqual(expectedMsg, msg);
                    await barrier.SignalAsync();
                };

                AsyncUtilities.ForEachAsync(async ep =>
                {
                    Message msg = new Message(module.Handler.Module, MSG_TYPE1, MSG + c.PostIncrement());

                    Assert.IsTrue(msgDic.TryAdd((module.Handler, ep).ToTuple(), msg));

                    await ep.SendAsync(msg);
                }, endpoints);
            }

            TestUtils.AssertTask(barrier.WaitAsync(), 1000000);
        }