コード例 #1
0
        public void SingleEventTest()
        {
            const string expected = "Hello, 普通话/普通話!";

            string message = "";

            server = new SimpleUDPServer(port, x =>
            {
                message = x;
            }, Encoding.UTF8);

            using (server)
            {
                server.Start();
                tailer.Start();

                testLog.WriteEntry(expected);

                int limit = 40;
                while (message == "" && limit-- > 0)
                {
                    Thread.Sleep(250);
                }
                Assert.Contains(expected, message);
                Assert.EndsWith("\n", message);
            }
        }
コード例 #2
0
        public void MultipleEventTest()
        {
            const string expected     = "Hello, 普通话/普通話!";
            const int    numMsgs      = 500;
            const int    sendInterval = 20; // 20MS

            List <string> msgs = new List <string>();

            server = new SimpleUDPServer(port, x =>
            {
                msgs.Add(x);
            }, Encoding.UTF8);

            using (server)
            {
                server.Start();
                tailer.Start();

                for (int i = 0; i < numMsgs; i++)
                {
                    testLog.WriteEntry(expected);
                    Thread.Sleep(sendInterval);
                }

                int limit = 40;
                while (msgs.Count < numMsgs && limit-- > 0)
                {
                    Thread.Sleep(250);
                }
                Assert.Equal(numMsgs, msgs.Count);
                foreach (string s in msgs)
                {
                    Assert.Contains(expected, s);
                    Assert.EndsWith("\n", s);
                }
            }
        }
コード例 #3
0
        public void CallsHandlerTest()
        {
            const string expectedMsg = "Is anybody there?";

            string actualMsg = "";
            bool   called    = false;

            SimpleUDPServer.DatagramReceiveHandler handler = (udpMsgStr) =>
            {
                actualMsg = udpMsgStr;
                called    = true;
            };

            const int port = 43431;

            using (SimpleUDPServer server = new SimpleUDPServer(port, handler))
            {
                server.Start();

                using (UdpClient client = new UdpClient(0))
                {
                    Byte[]     msg             = Encoding.Unicode.GetBytes(expectedMsg);
                    IPEndPoint localIPEndPoint = new IPEndPoint(IPAddress.Loopback, port);
                    client.Send(msg, msg.Length, localIPEndPoint);
                    client.Close();
                }

                int limit = 10;
                while (!called && --limit > 0)
                {
                    Thread.Sleep(100);
                }
                Assert.True(limit > 0);
                Assert.True(called);
                Assert.Equal(expectedMsg, actualMsg);
            }
        }