From f3671ea62d2605cd3e3b5ddd7c8cc2081d6dceb1 Mon Sep 17 00:00:00 2001 From: Solomon Hykes Date: Tue, 18 Mar 2014 17:21:32 -0700 Subject: [PATCH] Beam: basic test harness for the unix socket implementation. Docker-DCO-1.1-Signed-off-by: Solomon Hykes (github: shykes) --- beam/unix_test.go | 86 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 beam/unix_test.go diff --git a/beam/unix_test.go b/beam/unix_test.go new file mode 100644 index 0000000..bdd03b1 --- /dev/null +++ b/beam/unix_test.go @@ -0,0 +1,86 @@ +package beam + +import ( + "fmt" + "io/ioutil" + "testing" +) + +func TestSocketPair(t *testing.T) { + a, b, err := SocketPair() + if err != nil { + t.Fatal(err) + } + go func() { + a.Write([]byte("hello world!")) + fmt.Printf("done writing. closing\n") + a.Close() + fmt.Printf("done closing\n") + }() + data, err := ioutil.ReadAll(b) + if err != nil { + t.Fatal(err) + } + fmt.Printf("--> %s\n", data) + fmt.Printf("still open: %v\n", a.Fd()) +} + +func TestSendUnixSocket(t *testing.T) { + a1, a2, err := USocketPair() + if err != nil { + t.Fatal(err) + } + // defer a1.Close() + // defer a2.Close() + b1, b2, err := USocketPair() + if err != nil { + t.Fatal(err) + } + // defer b1.Close() + // defer b2.Close() + glueA, glueB, err := SocketPair() + if err != nil { + t.Fatal(err) + } + // defer glueA.Close() + // defer glueB.Close() + go func() { + err := Send(b2, []byte("a"), glueB) + if err != nil { + t.Fatal(err) + } + }() + go func() { + err := Send(a2, []byte("b"), glueA) + if err != nil { + t.Fatal(err) + } + }() + connAhdr, connA, err := Receive(a1) + if err != nil { + t.Fatal(err) + } + if string(connAhdr) != "b" { + t.Fatalf("unexpected: %s", connAhdr) + } + connBhdr, connB, err := Receive(b1) + if err != nil { + t.Fatal(err) + } + if string(connBhdr) != "a" { + t.Fatalf("unexpected: %s", connBhdr) + } + fmt.Printf("received both ends: %v <-> %v\n", connA.Fd(), connB.Fd()) + go func() { + fmt.Printf("sending message on %v\n", connA.Fd()) + connA.Write([]byte("hello world")) + connA.Sync() + fmt.Printf("closing %v\n", connA.Fd()) + connA.Close() + }() + data, err := ioutil.ReadAll(connB) + if err != nil { + t.Fatal(err) + } + fmt.Printf("---> %s\n", data) +}