Ejemplo n.º 1
0
 public void TestSaveLoadPrivateCacheItems()
 {
     using (Reference <ILogger> loggerRef = Reference <ILogger> .Create(new TraceLogger(true)))
         using (CoreServer server = new CoreServer(loggerRef, "UTT", NodeType.Router))
         {
             server.Start();
             using (ICoreClient client = new CoreClientFactory(loggerRef).SetEnv("UTT").Create())
                 using (ICoreCache cacheA = client.CreateCache())
                 {
                     Guid id = cacheA.SavePrivateObject("data", "name1", null);
                     //ICoreItem item = cacheA.LoadPrivateItem<string>("name1");
                     ICoreItem item = cacheA.LoadItem <string>("name1");
                     string    data = (string)item.Data;
                     Assert.AreEqual <string>("data", data);
                 }
         }
 }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            // exit codes:
            //   1  success
            //   0  failed (exception logged to network)
            //  -1  catastrophic failure (logged to local file)
            //  -2  catastrophic failure (logged to console only)
            using (ILogger logger = new ConsoleLogger("RequestHandler: "))
            {
                // argument defaults
                bool   attended     = false;
                Guid   requestId    = Guid.Empty;
                EnvId  env          = EnvId.Undefined;
                string hostInstance = null;
                // note: project debugging guid is: {6B70756E-16D8-4E9D-B104-3BE5830F229A}

                try
                {
                    // process arguments
                    logger.LogDebug("Arguments:");
                    for (int i = 0; i < args.Length; i++)
                    {
                        logger.LogDebug("  Argument[{0}]='{1}'", i, args[i]);
                        string[] argParts = args[i].Split(':');
                        string   argName  = argParts[0];
                        switch (argName.ToLower())
                        {
                        case "/debug": attended = true; break;

                        case "/reqid": requestId = Guid.Parse(argParts[1]); break;

                        case "/hiid":
                        {
                            hostInstance = argParts[1];
                            if (hostInstance.Equals("Default", StringComparison.OrdinalIgnoreCase))
                            {
                                hostInstance = null;
                            }
                        }
                        break;

                        case "/env": env = EnvHelper.ParseEnvName(argParts[1]); break;

                        default: break;
                        }
                    }

                    using (ICoreClient client = new CoreClientFactory(logger).SetEnv(env).Create())
                        //using (ILogger netLogger = new NetworkLogger(client, null))
                        using (ICoreCache cache = client.CreateCache())
                        {
                            //Environment.ExitCode = HandleRequest(new MultiLogger(logger, netLogger), cache, requestId);
                            Environment.ExitCode = RequestHandler.HandleRequest(logger, cache, requestId, hostInstance);
                        }
                }
                catch (Exception e1)
                {
                    // catastrophic failure
                    // - try to log to local file
                    try
                    {
                        Environment.ExitCode = -2;
                        using (ILogger errorLog = new MultiLogger(logger,
                                                                  new FileLogger(String.Format(@".\RequestHandler.Error.{0}.log", requestId))))
                        {
                            errorLog.LogDebug("Arguments:");
                            for (int i = 0; i < args.Length; i++)
                            {
                                errorLog.LogDebug("  Argument[{0}]='{1}'", i, args[i]);
                            }
                            errorLog.LogError(e1);
                        }
                    }
                    catch (Exception e2)
                    {
                        // I/O failure creating error log?
                        Environment.ExitCode = -3;
                        logger.LogDebug("Arguments ({0}):", args.Length);
                        for (int i = 0; i < args.Length; i++)
                        {
                            logger.LogDebug("  Argument[{0}]='{1}'", i, args[i]);
                        }
                        logger.LogError(e2);
                    }
                }
                if (attended)
                {
                    Console.WriteLine("Press ENTER to exit.");
                    Console.ReadLine();
                }
            }
        }
Ejemplo n.º 3
0
        public void TestCoreCaching()
        {
            using (Reference <ILogger> loggerRef = Reference <ILogger> .Create(new TraceLogger(true)))
                using (CoreServer server = new CoreServer(loggerRef, "UTT", NodeType.Router))
                {
                    server.Start();
                    using (ICoreClient client = new CoreClientFactory(loggerRef).SetEnv("UTT").Create())
                        using (ICoreCache cacheA = client.CreateCache())
                            using (ICoreCache cacheB = client.CreateCache())
                            {
                                using (cacheA.SubscribeNoWait <TestData>(Expr.ALL, null, null))
                                {
                                    using (cacheB.SubscribeNoWait <TestData>(Expr.ALL, null, null))
                                    {
                                        // publish an item and check all caches are consistent
                                        Guid id0 = cacheA.SaveObject <TestData>(new TestData("Zero", 0), "Item0", null, TimeSpan.MaxValue);

                                        // cacheA is publisher - should be immediately consistent
                                        Assert.AreEqual <int>(1, cacheA.ItemCount);
                                        ICoreItem item0a = cacheA.Items[0];
                                        Assert.IsNotNull(item0a);
                                        TestData data0a = (TestData)item0a.Data;
                                        Assert.IsNotNull(data0a);
                                        Assert.AreEqual <string>("Zero", (data0a.field1));

                                        // cacheB is not publisher - allow for propagation delay
                                        Thread.Sleep(500);
                                        // both caches should now be consistent
                                        Assert.AreEqual <int>(1, cacheA.ItemCount);
                                        Assert.AreEqual <int>(1, cacheB.ItemCount);
                                        ICoreItem item0b = cacheA.Items[0];
                                        Assert.IsNotNull(item0b);
                                        TestData data0b = (TestData)item0b.Data;
                                        Assert.IsNotNull(data0b);
                                        Assert.AreEqual <string>("Zero", (data0b.field1));
                                        Assert.AreEqual <Guid>(item0a.Id, item0b.Id);
                                    }

                                    // cacheB unsubscribed - should not receive updates from cacheA
                                    Guid      id1    = cacheA.SaveObject <TestData>(new TestData("One", 1), "Item1", null);
                                    ICoreItem data1a = cacheA.LoadItem <TestData>("Item1");
                                    Guid      id2    = cacheB.SaveObject <TestData>(new TestData("Two", 2), "Item2", null);
                                    ICoreItem data2b = cacheB.LoadItem <TestData>("Item2");
                                    // object is immediately available in local cache but not others
                                    Assert.IsNotNull(data1a);
                                    Assert.AreEqual <Guid>(id1, data1a.Id);
                                    Assert.IsNotNull(data2b);
                                    Assert.AreEqual <Guid>(id2, data2b.Id);
                                    // wait a bit for propagation
                                    Thread.Sleep(500);
                                    // both caches should now be 'up-to-date'
                                    // - A should receive from B, but not vice versa
                                    Assert.AreEqual <int>(3, cacheA.ItemCount);
                                    ICoreItem data2a = cacheA.LoadItem <TestData>("Item2");
                                    Assert.IsNotNull(data2a);
                                    Assert.AreEqual <Guid>(id2, data2a.Id);
                                    // - B should be inconsistent with A
                                    Assert.AreEqual <int>(2, cacheB.ItemCount);
                                    // - but becomes consistent as soon as we load the missing item
                                    ICoreItem data1b = cacheB.LoadItem <TestData>("Item1");
                                    Assert.AreEqual <int>(3, cacheB.ItemCount);
                                    Assert.IsNotNull(data1b);
                                    Assert.AreEqual <Guid>(id1, data1b.Id);
                                }
                            }
                }
        }