Ejemplo n.º 1
0
        protected static BasicAsyncService CreateAsyncService(BasicAsyncCallback callback)
        {
            var svc = new BasicAsyncService(callback);

            VerifyAsyncService(svc);
            return(svc);
        }
Ejemplo n.º 2
0
        public void Send_and_recv_works()
        {
            const double timeout = 100d;

            Given_two_connected_sockets((s1, s2) =>
            {
                Section($"async send and receive works", () =>
                {
                    int txSurplus = 0, rxDeficit = 1;

                    // Using the same Message throughout the body of this unit test.
                    using (var m = CreateMessage())
                    {
                        // Make sure that we are disposing the Services afterward.
                        BasicAsyncService txSvc = null;
                        BasicAsyncService rxSvc = null;

                        try
                        {
                            m.Body.Append(Hello);

                            // TODO: TBD: could have a base test class with CreateAsyncService...
                            // This is the kind of economy we want. Increased surplus, decreased deficit.
                            txSvc = CreateAsyncService(() => txSurplus++);
                            rxSvc = CreateAsyncService(() => -- rxDeficit);

                            Assert.True(txSvc.HasOne);
                            Assert.True(rxSvc.HasOne);

                            txSvc.Options.SetTimeoutDuration(FromMilliseconds(timeout));
                            rxSvc.Options.SetTimeoutDuration(FromMilliseconds(timeout));

                            Assert.Equal(0, txSurplus);
                            Assert.Equal(1, rxDeficit);

                            txSvc.Retain(m);
                            Assert.False(m.HasOne);

                            s2.ReceiveAsync(rxSvc);
                            s1.SendAsync(txSvc);

                            rxSvc.Wait();

                            Assert.True(txSvc.Success);
                            Assert.True(rxSvc.Success);

                            rxSvc.Cede(m);
                            Assert.True(m.HasOne);

                            Assert.Equal(Hello.ToBytes(), m.Body.Get());
                        }
                        finally
                        {
                            DisposeAll(txSvc, rxSvc);
                        }
                    }

                    // We like this kind of deficit spending.
                    Assert.Equal(1, txSurplus);
                    Assert.Equal(0, rxDeficit);
                });
            });
        }