Ejemplo n.º 1
0
        public void TestServerBridge()
        {
            // required environment:
            // - local env (eg. DEV) server
            // - lower env (eg. UTT) server
            // creates the bridge, and check all messages published
            //   in local env are re-published correctly in lower env
            EnvId  localEnvId   = EnvId.Dev_Development;
            string localEnvName = EnvHelper.EnvName(localEnvId);
            EnvId  lowerEnvId   = (localEnvId - 1);
            string lowerEnvName = EnvHelper.EnvName(lowerEnvId);
            int    localPort    = 9214;
            int    lowerPort    = 9114;

            using (Reference <ILogger> loggerRef = Reference <ILogger> .Create(new TraceLogger(true)))
            {
                CoreClientFactory clientfactory = new CoreClientFactory(loggerRef);
                using (CoreServer localServer =
                           new CoreServer(loggerRef, localEnvName, NodeType.Router, localPort))
                    using (CoreServer lowerServer =
                               new CoreServer(loggerRef, lowerEnvName, NodeType.Router, lowerPort))
                    {
                        // start servers
                        localServer.Start();
                        lowerServer.Start();
                        // create clients
                        using (Reference <ICoreClient> localClientRef = Reference <ICoreClient> .Create(clientfactory.SetEnv(localEnvName).SetServers("localhost:" + localPort).Create()))
                            using (ICoreClient lowerClient = clientfactory.SetEnv(lowerEnvName).SetServers("localhost:" + lowerPort).Create())
                            {
                                using (ServerBridge bridge = new ServerBridge())
                                {
                                    bridge.LoggerRef    = loggerRef;
                                    bridge.Client       = localClientRef;
                                    bridge.TargetClient = lowerClient;
                                    // test begins here
                                    // - start the bridge
                                    bridge.Start();
                                    const int sendCount      = 500;
                                    const int maxWaitSeconds = 5;
                                    long      excpCount      = 0;
                                    long      recdCount      = 0;
                                    // subscribe to objects on downstream server
                                    ISubscription subs = lowerClient.Subscribe <TestData>(Expr.ALL,
                                                                                          delegate(ISubscription subscription, ICoreItem item)
                                    {
                                        // receiver
                                        long count = Interlocked.Increment(ref recdCount);
                                        try
                                        {
                                            TestData data = (TestData)item.Data;
                                            //loggerRef.Target.LogDebug("Recd[{0}]", data.field2);
                                            Assert.AreEqual <long>(count, data.field2);
                                        }
                                        catch (Exception)
                                        {
                                            Interlocked.Increment(ref excpCount);
                                        }
                                    }, null);

                                    long sentCount = 0;
                                    // publish n Server events
                                    for (int i = 1; i <= sendCount; i++)
                                    {
                                        Interlocked.Increment(ref sentCount);
                                        localClientRef.Target.SaveObject <TestData>(new TestData("Test", i), "Test", null, TimeSpan.MaxValue);
                                    }
                                    // wait for a short period
                                    DateTimeOffset waitStart  = DateTimeOffset.Now;
                                    DateTimeOffset waitExpiry = waitStart.AddSeconds(maxWaitSeconds);
                                    while ((Interlocked.Add(ref recdCount, 0) < Interlocked.Add(ref sentCount, 0)) &&
                                           (DateTimeOffset.Now < waitExpiry) &&
                                           (Interlocked.Add(ref excpCount, 0) == 0))
                                    {
                                        Thread.Sleep(TimeSpan.FromSeconds(1.0));
                                        loggerRef.Target.LogDebug("Recd/Sent: {0}/{1} items...", Interlocked.Add(ref recdCount, 0), Interlocked.Add(ref sentCount, 0));
                                    }
                                    loggerRef.Target.LogDebug("Duration: {0}", (DateTimeOffset.Now - waitStart));
                                    Assert.AreEqual <long>(0, excpCount);
                                    Assert.AreEqual <long>(sendCount, recdCount);
                                    // done
                                    Assert.IsTrue(true);
                                }
                            }
                    }
            }
        }