File: foo_test.go

package info (click to toggle)
golang-github-influxdata-yarpc 0.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 276 kB
  • sloc: makefile: 2
file content (96 lines) | stat: -rw-r--r-- 1,810 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package foo

import (
	"context"
	"fmt"
	"net"
	"testing"

	"github.com/influxdata/yarpc"
)

type fooTestServer struct {
}

func (f *fooTestServer) UnaryMethod(ctx context.Context, in *Request) (*Response, error) {
	return &Response{"world"}, nil
}

func (f *fooTestServer) ServerStreamMethod(in *Request, stm Foo_ServerStreamMethodServer) error {
	for i := 0; i < 10; i++ {
		err := stm.Send(&Response{fmt.Sprintf("val %d", i)})
		if err != nil {
			println("server stream error", err.Error())
			return err
		}
	}
	return nil
}

func makeServer(t testing.TB) (l net.Listener, s *yarpc.Server, addr string) {
	var err error
	l, err = net.Listen("tcp", ":4040")
	if err != nil {
		t.Fatal("couldn't start listener", err)
	}

	s = yarpc.NewServer(yarpc.CustomCodec(&fooCodec{}))
	RegisterFooServer(s, &fooTestServer{})

	addr = l.Addr().String()
	return
}

func TestFooClient_UnaryMethod(t *testing.T) {
	l, s, _ := makeServer(t)

	go func() {
		s.Serve(l)
	}()
	defer s.Stop()

	cc, err := yarpc.Dial(":4040", yarpc.WithCodec(&fooCodec{}))
	if err != nil {
		t.Fatal("couldn't dial server", err)
	}

	fs := NewFooClient(cc)
	in := &Request{"hello"}
	val, err := fs.UnaryMethod(context.Background(), in)
	if err != nil {
		t.Errorf("unexpected error: %v", err)
	}
	t.Log(*val)
}

func TestFooClient_ServerStreamMethod(t *testing.T) {
	l, s, _ := makeServer(t)

	go func() {
		s.Serve(l)
	}()

	defer s.Stop()

	cc, err := yarpc.Dial(":4040", yarpc.WithCodec(&fooCodec{}))
	if err != nil {
		t.Fatal("couldn't dial server", err)
	}

	fs := NewFooClient(cc)
	in := &Request{"hello"}
	stream, err := fs.ServerStreamMethod(context.Background(), in)
	if err != nil {
		t.Errorf("unexpected error: %v", err)
	}

	for {
		val, err := stream.Recv()
		if err != nil {
			t.Log("EOF")
			break
		}

		t.Log(val.Out)
	}
}