public void OutgoingConnectionSendsMessageSuccessfully()
        {
            const string hostname             = "127.0.0.1";
            const int    port                 = 10100;
            const string connectionIdentifier = "1";

            var server = new SingleConnectionServer();

            server.StartListening();

            var engine = ExecutionEngineFactory.StartNew(new InMemoryStorageEngine());

            engine.Schedule(() =>
            {
                var unackedMessageQueue = new OutgoingMessageQueue();
                var deliverer           = new OutgoingMessageDeliverer(hostname, port, connectionIdentifier, unackedMessageQueue);

                Roots.Entangle(deliverer);
                Roots.Entangle(unackedMessageQueue);
                deliverer.Send("HELLO WORLD".GetUtf8Bytes());
            });

            Thread.Sleep(1_000);

            server.GetNodeIdentifier().ShouldBe("1");
            server.GetReceivedMessages()[0].Item2.ShouldBe("HELLO WORLD");

            var unackeds = engine.Schedule(() =>
            {
                var q = Roots.Resolve <OutgoingMessageQueue>();
                return(q.GetSyncedUnackedMessages().ToArray());
            }).Result;

            unackeds.Length.ShouldBe(1);
            unackeds[0].Item1.ShouldBe(0);
            unackeds[0].Item2.Array.ToUtf8String().ShouldBe("HELLO WORLD");

            server.AckUntil(0);

            Thread.Sleep(1_000);

            unackeds = engine.Schedule(() =>
            {
                var q = Roots.Resolve <OutgoingMessageQueue>();
                return(q.GetSyncedUnackedMessages().ToArray());
            }).Result;

            unackeds.Length.ShouldBe(0);
        }
Ejemplo n.º 2
0
        public void NonEmptyMessageQueueCanBeSerializeAndDeserialized()
        {
            var storage = new InMemoryStorageEngine();
            var c       = ExecutionEngineFactory.StartNew(storage);

            c.Schedule(() =>
            {
                var q         = new MessageQueue();
                var hello     = new ImmutableByteArray(Encoding.UTF8.GetBytes("hello"));
                var helloTask = new CTask();
                var world     = new ImmutableByteArray(Encoding.UTF8.GetBytes("world"));
                var worldTask = new CTask();
                q.Add(hello, helloTask);
                q.Add(world, worldTask);
                Roots.Entangle(q);
                Roots.Entangle(new CImmutableList <CTask>(new [] { helloTask, worldTask }));
            });

            c.Sync().Wait();

            c.Dispose();

            c = ExecutionEngineFactory.Continue(storage);
            c.Schedule(() =>
            {
                var queue = Roots.Resolve <MessageQueue>();
                var elms  = queue.GetAll().ToArray();
                elms.Length.ShouldBe(2);
                var s1 = Encoding.UTF8.GetString(elms[0].Array);
                s1.ShouldBe("hello");
                var s2 = Encoding.UTF8.GetString(elms[1].Array);
                s2.ShouldBe("world");

                Roots.Resolve <CImmutableList <CTask> >()[0].SignalCompletion();
            }).Wait();

            c.Sync().Wait();
            c.Dispose();

            c = ExecutionEngineFactory.Continue(storage);
            c.Schedule(() =>
            {
                var queue = Roots.Resolve <MessageQueue>();
                var elms  = queue.GetAll().ToArray();
                elms.Length.ShouldBe(1);
                var s1 = Encoding.UTF8.GetString(elms[0].Array);
                s1.ShouldBe("world");

                Roots.Resolve <CImmutableList <CTask> >()[1].SignalCompletion();
            }).Wait();

            c.Sync().Wait();
            c.Dispose();

            c = ExecutionEngineFactory.Continue(storage);
            c.Schedule(() =>
            {
                var queue = Roots.Resolve <MessageQueue>();
                var elms  = queue.GetAll().ToArray();
                elms.Length.ShouldBe(0);
            }).Wait();

            c.Sync().Wait();
            c.Dispose();
        }
 public static void Do <T>(this Engine scheduler, Action <T> action)
 => scheduler.Schedule(() => action(Roots.Resolve <T>()));
Ejemplo n.º 4
0
 public T Resolve <T>() => Roots.Resolve <T>();