public void ProxyAskCall_ReturnsCorrectValue(string s, int i, long l) { var func = new Func <string, int, long, string>((s1, i2, l3) => { return(s1 + i2.ToString() + l3.ToString()); }); var channelPair = TntTestHelper.CreateChannelPair(); var proxyConnection = TntBuilder .UseContract <ITestContract>() .UseReceiveDispatcher <ConveyorDispatcher>() .UseChannel(channelPair.CahnnelA) .Build(); var originConnection = TntBuilder .UseContract <ITestContract, TestContractMock>() .UseContractInitalization((c, _) => ((TestContractMock)c).WhenAskSILCalledCall(func)) .UseReceiveDispatcher <ConveyorDispatcher>() .UseChannel(channelPair.ChannelB) .Build(); channelPair.ConnectAndStartReceiving(); var proxyResult = proxyConnection.Contract.Ask(s, i, l); var originResult = func(s, i, l); Assert.AreEqual(originResult, proxyResult); }
public void ProxyAskCall_ReturnsSettedValue(string returnedValue) { var channelPair = TntTestHelper.CreateChannelPair(); var proxyConnection = TntBuilder .UseContract <ITestContract>() .UseReceiveDispatcher <ConveyorDispatcher>() .UseChannel(channelPair.CahnnelA) .Build(); var originConnection = TntBuilder .UseContract <ITestContract, TestContractMock>() .UseContractInitalization((c, _) => c.OnAskS += (arg) => arg) .UseReceiveDispatcher <ConveyorDispatcher>() .UseChannel(channelPair.ChannelB) .Build(); channelPair.ConnectAndStartReceiving(); //set 'echo' handler proxyConnection.Contract.OnAskS += (arg) => arg; //call var proxyResult = originConnection.Contract.OnAskS(returnedValue); Assert.AreEqual(returnedValue, proxyResult); }
public void Disconnected_duringOriginAsk_throws() { var channelPair = TntTestHelper.CreateChannelPair(); var originConnection = TntBuilder .UseContract <ITestContract, TestContractMock>() .UseReceiveDispatcher <NotThreadDispatcher>() .UseChannel(channelPair.ChannelB) .Build(); var proxyConnection = TntBuilder .UseContract <ITestContract>() .UseReceiveDispatcher <NotThreadDispatcher>() .UseContractInitalization((c, _) => { c.OnAsk += () => { channelPair.Disconnect(); try { //call say method to notify channel about disconnection (tcp channel immitation) originConnection.Contract.Say(); } catch (ConnectionIsLostException) { /*suppressTheException*/ } return(0); }; }) .UseChannel(channelPair.CahnnelA) .Build(); channelPair.ConnectAndStartReceiving(); TestTools.AssertThrowsAndNotBlocks <ConnectionIsLostException>(() => originConnection.Contract.OnAsk()); }
public void HundredOf2mbPacket_transmitsViaTcp_oneByOne() { var origin = TntBuilder .UseContract <ISingleMessageContract <Company>, SingleMessageContract <Company> >() .SetMaxAnsDelay(5 * 60 * 1000); var proxy = TntBuilder .UseContract <ISingleMessageContract <Company> >() .SetMaxAnsDelay(5 * 60 * 1000); using (var tcpPair = new TcpConnectionPair <ISingleMessageContract <Company>, ISingleMessageContract <Company>, SingleMessageContract <Company> >(origin, proxy)) { var receivedList = new List <Company>(200); tcpPair.OriginContract.SayCalled += (Sender, received) => receivedList.Add(received); var company = IntegrationTestsHelper.CreateCompany(2000); int sendCount = 100; for (int i = 0; i < sendCount; i++) { tcpPair.ProxyConnection.Contract.Ask(company); } Assert.AreEqual(sendCount, receivedList.Count); foreach (var received in receivedList) { received.AssertIsSameTo(company); } } }
public void UnDeserializeableContract_CreateT_throwsException() { var builder = TntBuilder .UseContract <IUnDeserializeableContract>() .UseChannel(new TestChannel()); Assert.Throws <TypeCannotBeDeserializedException>(() => builder.Build()); }
public void ServerCreatedServerIsNotListening() { var server = TntBuilder .UseContract <ITestContract, TestContractMock>() .UseReceiveDispatcher <NotThreadDispatcher>() .CreateTcpServer(IPAddress.Loopback, 12345); Assert.IsFalse(server.IsListening); }
public void OriginContract_CreatesByFactory_ContractCreated() { var proxyConnection = TntBuilder .UseContract <ITestContract, TestContractMock>() .UseChannel(new TestChannel()) .Build(); Assert.IsNotNull(proxyConnection.Contract); }
public void OriginContractAsInterface_CreatesByFactory_ContractCreated() { var proxyConnection = TntBuilder .UseContract <ITestContract, TestContractMock>() .UseChannel(new TestChannel()) .Build(); Assert.IsInstanceOf <ITestContract>(proxyConnection.Contract); }
public void ProxyConnectionIsNotEstablishedYet_AskCallThrows() { var proxyConnection = TntBuilder .UseContract <ITestContract>() .UseReceiveDispatcher <NotThreadDispatcher>() .UseChannel(() => new TcpChannel()) .Build(); TestTools.AssertThrowsAndNotBlocks <ConnectionIsNotEstablishedYet>(() => proxyConnection.Contract.Ask()); }
public void OriginContractAsSingleTone_CreatesByFactory_ContractCreated() { var contract = new TestContractMock(); var proxyConnection = TntBuilder .UseContract <ITestContract>(contract) .UseChannel(new TestChannel()) .Build(); Assert.AreEqual(contract, proxyConnection.Contract); }
public void ProxyBuilderCreatesWithCorrectConnection() { var channel = new TestChannel(); var proxyConnection = TntBuilder .UseContract <ITestContract>() .UseChannel(channel) .Build(); Assert.AreEqual(channel, proxyConnection.Channel); }
public void ServerAcceptConnection_AllowReceiveEqualTrue() { var server = new TestChannelServer <ITestContract>(TntBuilder.UseContract <ITestContract, TestContractMock>()); server.StartListening(); var clientChannel = new TestChannel(); TntBuilder.UseContract <ITestContract>().UseChannel(clientChannel).Build(); server.TestListener.ImmitateAccept(clientChannel); Assert.IsTrue(server.GetAllConnections().First().Channel.AllowReceive); }
public void ProxyBuilderBuilds_ChannelAllowReceiveIsTrue() { var channel = new TestChannel(); channel.ImmitateConnect(); var proxyConnection = TntBuilder .UseContract <ITestContract>() .UseChannel(channel) .Build(); Assert.IsTrue(channel.AllowReceive); }
public void ProxyBuilder_UseContractInitalization_CalledBeforeBuildDone() { var channel = new TestChannel(); ITestContract initializationArgument = null; var proxyConnection = TntBuilder.UseContract <ITestContract>() .UseContractInitalization((i, c) => initializationArgument = i) .UseChannel(channel) .Build(); Assert.IsNotNull(initializationArgument); Assert.AreEqual(proxyConnection.Contract, initializationArgument); }
public void ConnectionDisposes_channelBecomesDisconnected() { var channel = new TestChannel(); using (var proxyConnection = TntBuilder.UseContract <ITestContract>() .UseChannel(new TestChannel()) .Build()) { proxyConnection.Channel.ImmitateConnect(); } Assert.IsFalse(channel.IsConnected); }
public void ProxyConnectionIsLost_SayCallThrows() { var channel = new TestChannel(); var proxyConnection = TntBuilder .UseContract <ITestContract>() .UseReceiveDispatcher <NotThreadDispatcher>() .UseChannel(channel) .Build(); channel.ImmitateConnect(); channel.ImmitateDisconnect(); TestTools.AssertThrowsAndNotBlocks <ConnectionIsLostException>(() => proxyConnection.Contract.Say()); }
public void Run() { Console.WriteLine("TNT unit/integration test example."); Console.WriteLine("In this example, we will create connection with custom mock channell instead of using tcp."); Console.WriteLine("The example shows you how to write integration/unit tests at your code"); Console.WriteLine(); #region arrange var serverBuilder = TntBuilder .UseContract <IStage3EchoContract, Stage3EchoContract>() .UseReceiveDispatcher <NotThreadDispatcher>(); //Use "no thread dispatcher" to perform the calls in the calling thread var server = new TestChannelServer <IStage3EchoContract>(serverBuilder); server.StartListening(); var clientConnection = TntBuilder .UseContract <IStage3EchoContract>() .UseChannel(new TestChannel()) .Build(); //Immitate connection: server.TestListener.ImmitateAccept(clientConnection.Channel); #endregion #region act string testMessage = "Watup buddy?"; var echo = clientConnection.Contract.Send("superman", testMessage); #endregion #region assert // use // Assert.AreEqual(echo, testMessage) // with your test framework instead if (echo != testMessage) { throw new Exception("Unit test failed"); } else { Console.WriteLine("Integration test passed"); } #endregion Console.WriteLine("Press any key for exit..."); Console.ReadKey(); }
public void ServerAcceptConnection_AfterConnectRaised() { var server = new TestChannelServer <ITestContract>(TntBuilder.UseContract <ITestContract, TestContractMock>()); server.StartListening(); IConnection <ITestContract, TestChannel> incomeContractConnection = null; server.AfterConnect += (sender, income) => incomeContractConnection = income; var clientChannel = new TestChannel(); var proxyConnection = TntBuilder.UseContract <ITestContract>().UseChannel(clientChannel).Build(); server.TestListener.ImmitateAccept(clientChannel); Assert.IsNotNull(incomeContractConnection, "AfterConnect not raised"); }
public void Run() { Console.WriteLine("TNT easy start example."); Console.WriteLine("In this example, client sending messages to the server in stateless mode"); Console.WriteLine("No exception handling provided"); Console.WriteLine(); #region open the server Console.WriteLine("Opening the server..."); var server = TntBuilder .UseContract <IStage1Contract, Stage2Contract>() .CreateTcpServer(IPAddress.Any, 12345); /* * Uncomment folowing strings to see the order of operations: * * server.AfterConnect += * (_, c) => Console.WriteLine($"[Server] income connection from {c.Channel.RemoteEndpointName}"); * server.Disconnected += * (_, c) => Console.WriteLine($"[Server] disconnected {c.Connection.Channel.RemoteEndpointName}"); */ Console.WriteLine("Start listening..."); server.StartListening(); Console.WriteLine("listening is started"); #endregion Console.WriteLine("Type your messages or \"exit\" for exit:"); while (true) { var message = Console.ReadLine(); if (message.ToLower() == "exit") { break; } //Creating new client connection using (var client = TntBuilder .UseContract <IStage1Contract>() .CreateTcpClientConnection(IPAddress.Loopback, 12345)) { //Sending the message in "fire and foget" style, because the return type is void client.Contract.Send("Superman", message); } //Close connection right after the sending } server.Close(); }
public void HundredOf2mbPacket_transmitsViaTcp_concurent() { var origin = TntBuilder .UseContract <ISingleMessageContract <Company>, SingleMessageContract <Company> >() .SetMaxAnsDelay(5 * 60 * 1000); var proxy = TntBuilder .UseContract <ISingleMessageContract <Company> >() .SetMaxAnsDelay(5 * 60 * 1000); using (var tcpPair = new TcpConnectionPair <ISingleMessageContract <Company>, ISingleMessageContract <Company>, SingleMessageContract <Company> >(origin, proxy)) { var receivedList = new List <Company>(200); tcpPair.OriginContract.SayCalled += (Sender, received) => receivedList.Add(received); tcpPair.ClientChannel.OnDisconnect += (s, err) => { Console.WriteLine("asdasd"); }; var company = IntegrationTestsHelper.CreateCompany(2000); int sendCount = 100; List <Task> sendTasks = new List <Task>(sendCount); for (int i = 0; i < sendCount; i++) { sendTasks.Add(new Task(() => tcpPair.ProxyConnection.Contract.Ask(company))); } foreach (Task task in sendTasks) { task.Start(); } if (!Task.WaitAll(sendTasks.ToArray(), 5 * 60 * 1000)) { Assert.Fail("Test timeout "); } Assert.AreEqual(sendCount, receivedList.Count); foreach (var received in receivedList) { received.AssertIsSameTo(company); } } }
public void ProxyBuilder_SayCalled_DataSent() { var channel = new TestChannel(); var proxyConnection = TntBuilder .UseContract <ITestContract>() .UseChannel(channel) .Build(); proxyConnection.Channel.ImmitateConnect(); byte[] sentMessage = null; proxyConnection.Channel.OnWrited += (s, msg) => sentMessage = msg; proxyConnection.Contract.Say(); Assert.IsNotNull(sentMessage); Assert.IsNotEmpty(sentMessage); }
public void ClientDisconnected_DisconnectedRaised() { var server = new TestChannelServer <ITestContract>(TntBuilder.UseContract <ITestContract, TestContractMock>()); server.StartListening(); ClientDisconnectEventArgs <ITestContract, TestChannel> disconnectedConnection = null; server.Disconnected += (sender, args) => disconnectedConnection = args; var clientChannel = new TestChannel(); var proxyConnection = TntBuilder.UseContract <ITestContract>().UseChannel(clientChannel).Build(); var pair = server.TestListener.ImmitateAccept(clientChannel); pair.Disconnect(); Assert.IsNotNull(disconnectedConnection, "Disconnect not raised"); }
public void ServerAcceptConnection_BeforeConnectRaised() { var server = new TestChannelServer <ITestContract>(TntBuilder.UseContract <ITestContract, TestContractMock>()); server.StartListening(); BeforeConnectEventArgs <ITestContract, TestChannel> connectionArgs = null; server.BeforeConnect += (sender, args) => connectionArgs = args; var clientChannel = new TestChannel(); var proxyConnection = TntBuilder.UseContract <ITestContract>().UseChannel(clientChannel).Build(); server.TestListener.ImmitateAccept(clientChannel); Assert.IsNotNull(connectionArgs, "AfterConnect not raised"); }
/// <exception cref="SocketException"></exception> static void RunClient() { Console.WriteLine("Connecting to the server"); //Using statefull mode here: using (var client = TntBuilder // socket exception can be thrown here .UseContract <IStage2Contract>() //.UseContractInitalization((c,_)=>{}) place an additional initialization here .CreateTcpClientConnection(IPAddress.Loopback, 12345)) { //subscribing for income message callback: client.Contract.NewMessageReceived += (msg) => Console.WriteLine($"[{msg.Timestamp} {msg.User}] {msg.Message}"); if (!TryAuthorize(client)) { return; } Console.WriteLine("Type your messages or \"exit\" for exit:"); //Message sending loop: while (true) { var message = Console.ReadLine(); if (message.ToLower() == "exit") { break; } try { //Trying to send the message var messageId = client.Contract.Send(DateTime.Now, message); Console.WriteLine($"sent with id: {messageId}"); } catch (TntCallException e) { if (!client.Channel.IsConnected) { Console.WriteLine($"Disconnected because of {e.Message}"); } break; } } } }
public void Proxy_SayMissingCord_NotThrows() { var channelPair = TntTestHelper.CreateChannelPair(); var proxyConnection = TntBuilder .UseContract <ITestContract>() .UseReceiveDispatcher <NotThreadDispatcher>() .UseChannel(channelPair.CahnnelA) .Build(); var originConnection = TntBuilder .UseContract <IEmptyContract, EmptyContract>() .UseReceiveDispatcher <NotThreadDispatcher>() .UseChannel(channelPair.ChannelB) .Build(); channelPair.ConnectAndStartReceiving(); TestTools.AssertNotBlocks(proxyConnection.Contract.Say); }
public void Proxy_AskWithException_Throws() { var channelPair = TntTestHelper.CreateChannelPair(); var proxyConnection = TntBuilder .UseContract <IExceptionalContract>() .UseReceiveDispatcher <NotThreadDispatcher>() .UseChannel(channelPair.CahnnelA) .Build(); var originConnection = TntBuilder .UseContract <IExceptionalContract, ExceptionalContract>() .UseReceiveDispatcher <NotThreadDispatcher>() .UseChannel(channelPair.ChannelB) .Build(); channelPair.ConnectAndStartReceiving(); TestTools.AssertThrows_AndNotBlocks_AndContainsInfo <RemoteUnhandledException>(() => proxyConnection.Contract.Ask()); }
public Server() { _tntServer = TntBuilder //Using contract factory. ServerSideContractImplementation exemplar needs reference to broadcast method: .UseContract <IStage2Contract>(() => new Stage2ContractImplementation(this)) .CreateTcpServer(IPAddress.Any, 12345); //alow only 10 connections at the moment: _tntServer.BeforeConnect += (_, arg) => { if (_tntServer.ConnectionsCount >= 10) { arg.AllowConnection = false; } }; _tntServer.StartListening(); Console.WriteLine("Server opened"); }
public void Proxy_AskMissingCord_Throws() { var channelPair = TntTestHelper.CreateChannelPair(); var proxyConnection = TntBuilder .UseContract <ITestContract>() .UseReceiveDispatcher <NotThreadDispatcher>() .UseChannel(channelPair.CahnnelA) .Build(); var originConnection = TntBuilder .UseContract <IEmptyContract, EmptyContract>() .UseReceiveDispatcher <NotThreadDispatcher>() .UseChannel(channelPair.ChannelB) .Build(); channelPair.ConnectAndStartReceiving(); TestTools.AssertThrowsAndNotBlocks <RemoteContractImplementationException>(() => proxyConnection.Contract.Ask()); }
private static void TestLocalhost() { _output.WriteLine("-------------Localhost test--------------"); using (var server = TntBuilder .UseContract <ISpeedTestContract, SpeedTestContract>() .UseReceiveDispatcher <ConveyorDispatcher>() .CreateTcpServer(IPAddress.Loopback, 12345)) { server.StartListening(); using (var client = TntBuilder .UseContract <ISpeedTestContract>() .UseReceiveDispatcher <ConveyorDispatcher>() .CreateTcpClientConnection(IPAddress.Loopback, 12345)) { Test(client); } } }
static void Main(string[] args) { var server = TntBuilder .UseContract <IExampleContract, ExampleContract>() .CreateTcpServer(IPAddress.Loopback, 12345); server.StartListening(); Console.WriteLine("Type your messages:"); while (true) { var message = Console.ReadLine(); using (var client = TntBuilder.UseContract <IExampleContract>() .CreateTcpClientConnection(IPAddress.Loopback, 12345)) { client.Contract.Send("Superman", message); } } server.Close(); }