aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md34
-rw-r--r--pipeln.go4
2 files changed, 36 insertions, 2 deletions
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..bb07501
--- /dev/null
+++ b/README.md
@@ -0,0 +1,34 @@
+`pipeln` implements a trivial type, `PipeListenerDialer`, that can be
+used both as a `net.Listener` and as a dialer. It uses `net.Pipe` to
+connect server and clients so that testing client-server communication
+becomes easier.
+
+Several dialer methods are available, and happen (as there is no
+`net.Dialer` interface) to be compatible with both `net.Transport` and
+`grpc.WithContextDialer`.
+
+For instance:
+
+```go
+func TestHTTP(t *testing.T) {
+ ln := pipeln.New("test:80")
+
+ srv := http.Server{}
+ go srv.Serve(ln)
+
+ client := http.Client{Transport: &http.Transport{Dial: ln.Dial}}
+
+ // ...
+}
+
+func TestGRPC(t *testing.T) {
+ ln := pipeln.New("test")
+
+ srv := grpc.NewServer()
+ go srv.Serve(ln)
+
+ client, _ := grpc.Dial("test", grpc.WithContextDialer(ln.DialContextAddr), grpc.WithInsecure())
+
+ // ...
+}
+```
diff --git a/pipeln.go b/pipeln.go
index 507ab33..c836f28 100644
--- a/pipeln.go
+++ b/pipeln.go
@@ -62,10 +62,10 @@ func (ln *PipeListenerDialer) Dial(_, addr string) (net.Conn, error) {
}
s, c := net.Pipe()
select {
- case <-ln.done:
- return nil, ErrClosed
case ln.conns <- s:
return c, nil
+ case <-ln.done:
+ return nil, ErrClosed
}
}