func TestPair(t *testing.T) { r, w, err := Pair() if err != nil { t.Fatal("Unexpected error") } defer r.Close() defer w.Close() testutils.Timeout(t, func() { go func() { msg, err := r.Receive(0) if err != nil { t.Fatal(err) } if msg.Verb != beam.Log { t.Fatalf("%#v", *msg) } if msg.Args[0] != "hello world" { t.Fatalf("%#v", *msg) } }() _, err := w.Send(&beam.Message{Verb: beam.Log, Args: []string{"hello world"}}) if err != nil { t.Fatal(err) } }) }
func TestStackWithPair(t *testing.T) { r, w, err := unix.Pair() if err != nil { t.Fatal(err) } defer r.Close() defer w.Close() s := NewStackSender() s.Add(w) testutils.Timeout(t, func() { go func() { msg, _, _, err := r.Receive(0) if err != nil { t.Fatal(err) } if msg.Name != "hello" { t.Fatalf("%#v", msg) } if strings.Join(msg.Args, " ") != "wonderful world" { t.Fatalf("%#v", msg) } }() _, _, err := s.Send(&beam.Message{"hello", []string{"wonderful", "world"}}, 0) if err != nil { t.Fatal(err) } }) }
func TestSendFile(t *testing.T) { r, w := Pipe() defer r.Close() defer w.Close() tmp, err := ioutil.TempFile("", "libchan-test-") if err != nil { t.Fatal(err) } defer os.RemoveAll(tmp.Name()) fmt.Fprintf(tmp, "hello world\n") tmp.Sync() tmp.Seek(0, 0) testutils.Timeout(t, func() { go func() { _, err := w.Send(&Message{Data: []byte("path=" + tmp.Name()), Fd: tmp}) if err != nil { t.Fatal(err) } }() msg, err := r.Receive(0) if err != nil { t.Fatal(err) } if string(msg.Data) != "path="+tmp.Name() { t.Fatalf("%#v", msg) } txt, err := ioutil.ReadAll(msg.Fd) if err != nil { t.Fatal(err) } if string(txt) != "hello world\n" { t.Fatalf("%s\n", txt) } }) }
func TestPair(t *testing.T) { r, w, err := Pair() if err != nil { t.Fatal("Unexpected error") } defer r.Close() defer w.Close() testutils.Timeout(t, func() { go func() { msg, in, out, err := r.Receive(0) if err != nil { t.Fatal(err) } if msg.Name != "print" { t.Fatalf("%#v", *msg) } if msg.Args[0] != "hello world" { t.Fatalf("%#v", *msg) } if in != nil && out != nil { t.Fatal("Unexpected return value") } }() _, _, err := w.Send(&beam.Message{Name: "print", Args: []string{"hello world"}}, 0) if err != nil { t.Fatal(err) } }) }
func TestSimpleSend(t *testing.T) { r, w := Pipe() defer r.Close() defer w.Close() testutils.Timeout(t, func() { go func() { msg, in, out, err := r.Receive(0) if err != nil { t.Fatal(err) } if msg.Name != "print" { t.Fatalf("%#v", *msg) } if msg.Args[0] != "hello world" { t.Fatalf("%#v", *msg) } assertMode(t, in, out, 0) }() in, out, err := w.Send(&beam.Message{Name: "print", Args: []string{"hello world"}}, 0) if err != nil { t.Fatal(err) } assertMode(t, in, out, 0) }) }
func TestStackWithPipe(t *testing.T) { r, w := beam.Pipe() defer r.Close() defer w.Close() s := NewStackSender() s.Add(w) testutils.Timeout(t, func() { go func() { msg, err := r.Receive(0) if err != nil { t.Fatal(err) } if msg.Verb != beam.Log { t.Fatalf("%#v", msg) } if strings.Join(msg.Args, " ") != "wonderful world" { t.Fatalf("%#v", msg) } }() _, err := s.Send(&beam.Message{Verb: beam.Log, Args: []string{"wonderful", "world"}}) if err != nil { t.Fatal(err) } }) }
func TestStackWithPair(t *testing.T) { r, w, err := unix.Pair() if err != nil { t.Fatal(err) } defer r.Close() defer w.Close() s := NewStackSender() s.Add(w) testutils.Timeout(t, func() { go func() { msg, err := r.Receive(0) if err != nil { t.Fatal(err) } if string(msg.Data) != "Log" { t.Fatalf("%#v", msg) } }() _, err := s.Send(&libchan.Message{Data: []byte("Log")}) if err != nil { t.Fatal(err) } }) }
func TestSimpleSend(t *testing.T) { r, w := Pipe() defer r.Close() defer w.Close() testutils.Timeout(t, func() { go func() { msg, err := r.Receive(0) if err != nil { t.Fatal(err) } if string(msg.Data) != "hello world" { t.Fatalf("%#v", *msg) } }() if _, err := w.Send(&Message{Data: []byte("hello world")}); err != nil { t.Fatal(err) } }) }
func TestSendReply(t *testing.T) { r, w := Pipe() defer r.Close() defer w.Close() testutils.Timeout(t, func() { // Send go func() { ret, err := w.Send(&Message{Data: []byte("this is the request"), Ret: RetPipe}) if err != nil { t.Fatal(err) } if ret == nil { t.Fatalf("ret = nil\n") } // Read for a reply msg, err := ret.Receive(0) if err != nil { t.Fatal(err) } if string(msg.Data) != "this is the reply" { t.Fatalf("%#v", msg) } }() // Receive a message with mode=Ret msg, err := r.Receive(Ret) if err != nil { t.Fatal(err) } if string(msg.Data) != "this is the request" { t.Fatalf("%#v", msg) } if msg.Ret == nil { t.Fatalf("%#v", msg) } // Send a reply _, err = msg.Ret.Send(&Message{Data: []byte("this is the reply")}) if err != nil { t.Fatal(err) } }) }
func TestSendNested(t *testing.T) { r, w, err := Pair() if err != nil { t.Fatal(err) } defer r.Close() defer w.Close() testutils.Timeout(t, func() { // Send go func() { // Send a message with mode=W in, out, err := w.Send(&beam.Message{Args: []string{"this is the request"}}, beam.W) if err != nil { t.Fatal(err) } assertMode(t, in, out, beam.W) // Send a nested message _, _, err = out.Send(&beam.Message{Args: []string{"this is the nested message"}}, 0) if err != nil { t.Fatal(err) } }() // Receive a message with mode=R msg, in, out, err := r.Receive(beam.R) if err != nil { t.Fatal(err) } if msg.Args[0] != "this is the request" { t.Fatalf("%#v", msg) } assertMode(t, in, out, beam.R) // Read for a nested message nested, _, _, err := in.Receive(0) if err != nil { t.Fatal(err) } if nested.Args[0] != "this is the nested message" { t.Fatalf("%#v", nested) } }) }
func TestSendReply(t *testing.T) { r, w, err := Pair() if err != nil { t.Fatal(err) } defer r.Close() defer w.Close() testutils.Timeout(t, func() { // Send go func() { // Send a message with mode=R ret, err := w.Send(&beam.Message{Args: []string{"this is the request"}, Ret: beam.RetPipe}) if err != nil { t.Fatal(err) } // Read for a reply msg, err := ret.Receive(0) if err != nil { t.Fatal(err) } if msg.Args[0] != "this is the reply" { t.Fatalf("%#v", msg) } }() // Receive a message with mode=W msg, err := r.Receive(beam.Ret) if err != nil { t.Fatal(err) } if msg.Args[0] != "this is the request" { t.Fatalf("%#v", msg) } // Send a reply _, err = msg.Ret.Send(&beam.Message{Args: []string{"this is the reply"}}) if err != nil { t.Fatal(err) } }) }
func testRemote(t *testing.T, senderSide, receiverSide func(*Engine)) { sndConn, rcvConn, err := beam.USocketPair() if err != nil { t.Fatal(err) } defer sndConn.Close() defer rcvConn.Close() sender := NewSender(sndConn) receiver := NewReceiver(rcvConn) // Setup the sender side eng := New() sender.Install(eng) // Setup the receiver side receiverSide(receiver.Engine) go receiver.Run() testutils.Timeout(t, func() { senderSide(eng) }) }
func TestSimpleSend(t *testing.T) { r, w := Pipe() defer r.Close() defer w.Close() testutils.Timeout(t, func() { go func() { msg, err := r.Receive(0) if err != nil { t.Fatal(err) } if msg.Verb != Log { t.Fatalf("%#v", *msg) } if msg.Args[0] != "hello world" { t.Fatalf("%#v", *msg) } }() if _, err := w.Send(&Message{Verb: Log, Args: []string{"hello world"}}); err != nil { t.Fatal(err) } }) }
func TestSendReply(t *testing.T) { r, w := Pipe() defer r.Close() defer w.Close() testutils.Timeout(t, func() { // Send go func() { // Send a message with mode=R in, out, err := w.Send(&beam.Message{Args: []string{"this is the request"}}, beam.R) if err != nil { t.Fatal(err) } assertMode(t, in, out, beam.R) // Read for a reply resp, _, _, err := in.Receive(0) if err != nil { t.Fatal(err) } if resp.Args[0] != "this is the reply" { t.Fatalf("%#v", resp) } }() // Receive a message with mode=W msg, in, out, err := r.Receive(beam.W) if err != nil { t.Fatal(err) } if msg.Args[0] != "this is the request" { t.Fatalf("%#v", msg) } assertMode(t, in, out, beam.W) // Send a reply _, _, err = out.Send(&beam.Message{Args: []string{"this is the reply"}}, 0) if err != nil { t.Fatal(err) } }) }
func TestSendFile(t *testing.T) { r, w := Pipe() defer r.Close() defer w.Close() tmp, err := ioutil.TempFile("", "beam-test-") if err != nil { t.Fatal(err) } defer os.RemoveAll(tmp.Name()) fmt.Fprintf(tmp, "hello world\n") tmp.Sync() tmp.Seek(0, 0) testutils.Timeout(t, func() { go func() { _, err := w.Send(&Message{Verb: File, Args: []string{"path=" + tmp.Name()}, Att: tmp}) if err != nil { t.Fatal(err) } }() msg, err := r.Receive(0) if err != nil { t.Fatal(err) } if msg.Verb != File { t.Fatalf("%#v", msg) } if msg.Args[0] != "path="+tmp.Name() { t.Fatalf("%#v", msg) } txt, err := ioutil.ReadAll(msg.Att) if err != nil { t.Fatal(err) } if string(txt) != "hello world\n" { t.Fatalf("%s\n", txt) } }) }
func TestPair(t *testing.T) { r, w, err := Pair() if err != nil { t.Fatal("Unexpected error") } defer r.Close() defer w.Close() testutils.Timeout(t, func() { go func() { msg, err := r.Receive(0) if err != nil { t.Fatal(err) } if string(msg.Data) != "hello world" { t.Fatalf("%#v", *msg) } }() _, err := w.Send(&lch.Message{Data: []byte("hello world")}) if err != nil { t.Fatal(err) } }) }