Exemplo n.º 1
0
        public static IDisposable StartService(TestDetails testDetails, TestCallbackHandler testCallback)
        {
            if (_currentService != null)
            {
                _currentService.Dispose();
                _currentService = null;
            }

            _currentCallback = testCallback;
            _testDetails = testDetails;

            ServiceHost host = new ServiceHost(typeof(TestCallbackService));
            try
            {
                var binding = new NetNamedPipeBinding();
                binding.MaxReceivedMessageSize = 100000;
                host.AddServiceEndpoint(typeof(ITestCallbackService), binding, new Uri(ServiceAddress));
                host.Open();
            }
            catch (Exception)
            {
                ((IDisposable)host).Dispose();
                _currentCallback = null;
                throw;
            }
            return new ServiceWrapper(host);
        }
Exemplo n.º 2
0
        public void TestCallbackServer()
        {
            var callbackServer  = new CallbackServer(_mockJvm.Object, false);
            var callbackHandler = new TestCallbackHandler();

            callbackHandler.Id = callbackServer.RegisterCallback(callbackHandler);
            Assert.Equal(1, callbackHandler.Id);

            using ISocketWrapper callbackSocket = SocketFactory.CreateSocket();
            callbackServer.Run(callbackSocket);

            int connectionNumber = 2;
            var clientSockets    = new ISocketWrapper[connectionNumber];

            for (int i = 0; i < connectionNumber; ++i)
            {
                var            ipEndpoint   = (IPEndPoint)callbackSocket.LocalEndPoint;
                ISocketWrapper clientSocket = SocketFactory.CreateSocket();
                clientSockets[i] = clientSocket;
                clientSocket.Connect(ipEndpoint.Address, ipEndpoint.Port);

                WriteAndReadTestData(clientSocket, callbackHandler, i);
            }

            Assert.Equal(connectionNumber, callbackServer.CurrentNumConnections);

            IOrderedEnumerable <int> actualValues   = callbackHandler.Inputs.OrderBy(i => i);
            IEnumerable <int>        expectedValues = Enumerable
                                                      .Range(0, connectionNumber)
                                                      .Select(i => callbackHandler.Apply(i))
                                                      .OrderBy(i => i);

            Assert.True(expectedValues.SequenceEqual(actualValues));
        }
Exemplo n.º 3
0
 public void TestCallbackHandlers()
 {
     var tokenSource          = new CancellationTokenSource();
     var callbackHandlersDict = new ConcurrentDictionary <int, ICallbackHandler>();
     int inputToHandler       = 1;
     {
         // Test CallbackConnection using a ICallbackHandler that runs
         // normally without error.
         var callbackHandler = new TestCallbackHandler
         {
             Id = 1
         };
         callbackHandlersDict[callbackHandler.Id] = callbackHandler;
         TestCallbackConnection(
             callbackHandlersDict,
             callbackHandler,
             inputToHandler,
             tokenSource.Token);
         Assert.Single(callbackHandler.Inputs);
         Assert.Equal(
             callbackHandler.Apply(inputToHandler),
             callbackHandler.Inputs.First());
     }
     {
         // Test CallbackConnection using a ICallbackHandler that
         // throws an exception.
         var callbackHandler = new ThrowsExceptionHandler
         {
             Id = 2
         };
         callbackHandlersDict[callbackHandler.Id] = callbackHandler;
         TestCallbackConnection(
             callbackHandlersDict,
             callbackHandler,
             inputToHandler,
             tokenSource.Token);
         Assert.Empty(callbackHandler.Inputs);
     }
     {
         // Test CallbackConnection when cancellation has been requested for the token.
         tokenSource.Cancel();
         var callbackHandler = new TestCallbackHandler
         {
             Id = 3
         };
         callbackHandlersDict[callbackHandler.Id] = callbackHandler;
         TestCallbackConnection(
             callbackHandlersDict,
             callbackHandler,
             inputToHandler,
             tokenSource.Token);
         Assert.Empty(callbackHandler.Inputs);
     }
 }
Exemplo n.º 4
0
        public void AsyncProducerSendsMessageWithCallback()
        {
            var prodConfig = this.AsyncProducerConfig1;

            var messages = new List <Message>
            {
                new Message(Encoding.UTF8.GetBytes("Async Test Message 1")),
            };
            var myHandler = new TestCallbackHandler();

            using (var producer = new AsyncProducer(prodConfig))
            {
                producer.Send(CurrentTestTopic, 0, messages, myHandler.Handle);
            }

            Thread.Sleep(1000);
            Assert.IsTrue(myHandler.WasRun);
        }
        public void WhenStartedWithTestDataDirShouldRemoveTwoFiles()
        {
            string targetDir = @"C:\Project\TestData\Temp\" + Guid.NewGuid().ToString();

            Directory.CreateDirectory(targetDir);

            List <string> fullFileList = CopyTestDataDir(@"C:\Project\TestData\PhotoSpaceSaver", targetDir);

            var sut = new PhotoSpaceSaver.Core.Logic.PhotoSpaceSaver();
            var testCallbackHandler = new TestCallbackHandler();

            sut.StartAsync(new List <string> {
                targetDir
            }, accessToken, testCallbackHandler).Wait();
            Assert.AreEqual(fullFileList.Count, testCallbackHandler.deletedFiles.Count);


            //Directory.Delete(targetDir, true);
        }
Exemplo n.º 6
0
        public async Task TestCallbackIds()
        {
            int numToRegister   = 100;
            var callbackServer  = new CallbackServer(_mockJvm.Object, false);
            var callbackHandler = new TestCallbackHandler();

            var ids   = new ConcurrentBag <int>();
            var tasks = new List <Task>();

            for (int i = 0; i < numToRegister; ++i)
            {
                tasks.Add(
                    Task.Run(() => ids.Add(callbackServer.RegisterCallback(callbackHandler))));
            }

            await Task.WhenAll(tasks);

            IOrderedEnumerable <int> actualIds   = ids.OrderBy(i => i);
            IEnumerable <int>        expectedIds = Enumerable.Range(1, numToRegister);

            Assert.True(expectedIds.SequenceEqual(actualIds));
        }
Exemplo n.º 7
0
 public void AsyncProducerSendsMessageWithCallbackClass()
 {
     List<Message> messages = new List<Message>()
                                  {
                                      new Message(Encoding.UTF8.GetBytes("Async Test Message 1")),
                                  };
     var config = new AsyncProducerConfig(clientConfig);
     TestCallbackHandler myHandler = new TestCallbackHandler();
     var producer = new AsyncProducer(config, myHandler);
     producer.Send(CurrentTestTopic, 0, messages);
     Thread.Sleep(1000);
     Assert.IsTrue(myHandler.WasRun);
 }
Exemplo n.º 8
0
        public void AsyncProducerSendsMessageWithCallbackClass()
        {
            var prodConfig = this.AsyncProducerConfig1;

            var messages = new List<Message>
                                         {
                                             new Message(Encoding.UTF8.GetBytes("Async Test Message 1")),
                                         };
            var myHandler = new TestCallbackHandler();
            using (var producer = new AsyncProducer(prodConfig, myHandler))
            {
                producer.Send(CurrentTestTopic, 0, messages);
            }

            Thread.Sleep(1000);
            Assert.IsTrue(myHandler.WasRun);
        }