5Flags
5Flags

Reputation: 133

Within-process Communication with gRPC

To facilitate some testing, I'd like to be able to create both server and client parts of the gRPC service I've written in a test, and have them communicate in-process, rather than through a TCP connection.

The basic motivation is to avoid flaky tests setting up and tearing down TCP ports in NUnit. I can't see any obvious way of doing this in C# with gRPC. Is it possible?

Upvotes: 1

Views: 946

Answers (2)

dan-kli
dan-kli

Reputation: 893

I know this answer probably comes a bit late, but I just came across this question, since I am currently implementing what you asked for.

Inter-process communication is now possible with C# in gRPC between clients and server, for example via Unix sockets, check out this article and the Microsoft Documentation here for example code.

Upvotes: 1

Marc Gravell
Marc Gravell

Reputation: 1063664

In theory, yes; in reality: as far as I know, nobody has implemented this kind of null transport channel in .NET - I'd quite like one too :) The problem is that it would also need to spoof a lot of HTTP/2 semantics, since gRPC is unashamedly tied to HTTP/2 concepts. For now, I tend to just use a fixture that provides multiple services on the same endpoint, and spin up the server in the fixture (and tear it down in the fixture dispose); it works well enough for my tests to be pretty stable.

This might be worth pursuing more if it also worked, for example, against named pipes - to allow lightweight IPC (relative to full HTTP/2) on the same machine, but... it is a non-trivial amount of work for an unquantifiable benefit, and the "just spin up a server in a fixture" approach works well enough.

Upvotes: 1

Related Questions