tsJensen

A quest for software excellence...

ServiceWire and .NET Core Integration Tests

I’m very happy with the quick and dirty .NET Core integration test code I’ve just committed to GitHub for ServiceWire. The only changes to existing integration test code were these:

Removal of Named Pipes from the test since .NET Core does not support Named Pipes (so far as I can learn) as Named Pipes is a Windows only thing (again, so far as I have learned).

Modified code to get IP address and port from command line args rather than configuration along with some defaults.

So here’s the primary host host code. Note how easy it is to host implementations for multiple interfaces.

class Program
{
   static void Main(string[] args)
   {
      var logger = new Logger(logLevel: LogLevel.Debug);
      var stats = new Stats();

      var addr = new[] { "127.0.0.1", "8098" }; //defaults
      if (null != args && args.Length > 0)
      {
         var parts = args[0].Split(':');
         if (parts.Length > 1) addr[1] = parts[1];
         addr[0] = parts[0];
      }

      var ip = addr[0];
      var port = Convert.ToInt32(addr[1]);
      var ipEndpoint = new IPEndPoint(IPAddress.Any, port);

      var useCompression = false;
      var compressionThreshold = 131072; //128KB

      var tester = new NetTester();
      var mytester = new MyTester();

      var tcphost = new TcpHost(ipEndpoint, logger, stats);
      tcphost.UseCompression = useCompression;
      tcphost.CompressionThreshold = compressionThreshold;
      tcphost.AddService<INetTester>(tester);
      tcphost.AddService<IMyTester>(mytester);

      var valTypes = new ValTypes();
      tcphost.AddService<IValTypes>(valTypes);

      tcphost.Open();

      Console.WriteLine("Press Enter to stop the dual host test.");
      Console.ReadLine();

      tcphost.Close();

      Console.WriteLine("Press Enter to quit.");
      Console.ReadLine();
   }
}

And here’s the client code.

class Program
{
   private static void Main(string[] args)
   {
      var addr = new[] { "127.0.0.1", "8098" }; //defaults
      if (null != args && args.Length > 0)
      {
         var parts = args[0].Split(':');
         if (parts.Length > 1) addr[1] = parts[1];
         addr[0] = parts[0];
      }

      var ip = addr[0];
      var port = Convert.ToInt32(addr[1]);
      var ipEndpoint = new IPEndPoint(IPAddress.Parse(ip), port);
      for (int i = 0; i < 1; i++) RunTest(ipEndpoint, ip);

      Console.ReadLine();
   }

   private static void RunTest(IPEndPoint ipEndpoint, string ip)
   {
      using (var client = new TcpClient<IValTypes>(ipEndpoint))
      {
         decimal abc = client.Proxy.GetDecimal(4.5m);
         bool result = client.Proxy.OutDecimal(abc);
      }

      using (var client = new NetTcpTesterProxy(ipEndpoint))
      {
         var id = client.GetId("test1", 3.314, 42, DateTime.Now);
         long q = 3;
         var response = client.Get(id, "mirror", 4.123, out q);
         var list = client.GetItems(id);
      }
      using (var client = new NetTcpMyTesterProxy(ipEndpoint))
      {
         var id = client.GetId("test1", 3.314, 42);
         int q2 = 4;
         var response = client.Get(id, "mirror", 4.123, out q2);
         var list = client.GetItems(id, new int[] { 3, 6, 9 });
      }

      var sw = Stopwatch.StartNew();
      var from = 0;
      var to = 400;
      Parallel.For(from, to, index =>
      {
         using (var client = new NetTcpTesterProxy(ipEndpoint))
         {
            for (int i = 0; i < 10; i++)
            {
               var id = client.GetId("test1", 3.314, 42, DateTime.Now);
               long q = 2;
               var response = client.Get(id, "mirror", 4.123, out q);
               var list = client.GetItems(id);
            }
         }

         using (var client = new NetTcpMyTesterProxy(ipEndpoint))
         {
            for (int i = 0; i < 10; i++)
            {
               var id = client.GetId("test1", 3.314, 42);
               int q2 = 6;
               var response = client.Get(id, "mirror", 4.123, out q2);
               var list = client.GetItems(id, new int[] { 3, 6, 9 });
            }
         }
      });
      sw.Stop();
      var msperop = sw.ElapsedMilliseconds / 24000.0;
      Console.WriteLine("tcp: {0}, {1}", sw.ElapsedMilliseconds, msperop);
   }
}

It’s quite simple, but I’m going to be working on both host and client code to make it easier to code up both of them with some convention assumptions that will also not break existing host and client code. After that, I’ll publish a new NuGet package with a major version rev to 5.0.