blob: e7528e328631feadd9de89c50229ad83a85d4f9a [file] [log] [blame]
Jiri Simsad7616c92015-03-24 23:44:30 -07001// Copyright 2015 The Vanadium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
Suharsh Sivakumarcbfe4742015-03-11 14:54:22 -07005package test
Cosmos Nicolaouae8dd212014-12-13 23:43:08 -08006
7import (
8 "io"
9 "testing"
10 "time"
11
Jiri Simsa6ac95222015-02-23 16:11:49 -080012 "v.io/v23"
Todd Wang54feabe2015-04-15 23:38:26 -070013 "v.io/v23/context"
Matt Rosencrantz94502cf2015-03-18 09:43:44 -070014 "v.io/v23/rpc"
Bogdan Caprita95bca142015-06-29 17:16:05 -070015 "v.io/x/ref/test"
Cosmos Nicolaouae8dd212014-12-13 23:43:08 -080016)
17
18type simple struct {
19 done <-chan struct{}
20}
21
Todd Wang54feabe2015-04-15 23:38:26 -070022func (s *simple) Sleep(*context.T, rpc.ServerCall) error {
Cosmos Nicolaouae8dd212014-12-13 23:43:08 -080023 select {
24 case <-s.done:
25 case <-time.After(time.Hour):
26 }
27 return nil
28}
29
Todd Wang54feabe2015-04-15 23:38:26 -070030func (s *simple) Ping(_ *context.T, _ rpc.ServerCall) (string, error) {
Cosmos Nicolaouae8dd212014-12-13 23:43:08 -080031 return "pong", nil
32}
33
Todd Wang54feabe2015-04-15 23:38:26 -070034func (s *simple) Echo(_ *context.T, _ rpc.ServerCall, arg string) (string, error) {
Cosmos Nicolaou185c0c62015-04-13 21:22:43 -070035 return arg, nil
36}
37
Todd Wang54feabe2015-04-15 23:38:26 -070038func (s *simple) Source(_ *context.T, call rpc.StreamServerCall, start int) error {
Cosmos Nicolaouae8dd212014-12-13 23:43:08 -080039 i := start
40 backoff := 25 * time.Millisecond
41 for {
42 select {
43 case <-s.done:
44 return nil
45 case <-time.After(backoff):
46 call.Send(i)
47 i++
48 }
49 backoff *= 2
50 }
51}
52
Todd Wang54feabe2015-04-15 23:38:26 -070053func (s *simple) Sink(_ *context.T, call rpc.StreamServerCall) (int, error) {
Cosmos Nicolaouae8dd212014-12-13 23:43:08 -080054 i := 0
55 for {
56 if err := call.Recv(&i); err != nil {
Todd Wange77f9952015-02-18 13:20:50 -080057 if err == io.EOF {
58 return i, nil
59 }
60 return 0, err
Cosmos Nicolaouae8dd212014-12-13 23:43:08 -080061 }
62 }
63}
64
Todd Wang54feabe2015-04-15 23:38:26 -070065func (s *simple) Inc(_ *context.T, call rpc.StreamServerCall, inc int) (int, error) {
Cosmos Nicolaouae8dd212014-12-13 23:43:08 -080066 i := 0
67 for {
68 if err := call.Recv(&i); err != nil {
69 if err == io.EOF {
Todd Wange77f9952015-02-18 13:20:50 -080070 return i, nil
Cosmos Nicolaouae8dd212014-12-13 23:43:08 -080071 }
Todd Wange77f9952015-02-18 13:20:50 -080072 return 0, err
Cosmos Nicolaouae8dd212014-12-13 23:43:08 -080073 }
74 call.Send(i + inc)
75 }
76}
77
78func TestSimpleRPC(t *testing.T) {
Adam Sadovsky623bed02015-10-30 11:02:43 -070079 ctx, shutdown := test.V23InitWithMounttable()
Ankurfb9255a2015-01-21 15:29:38 -080080 defer shutdown()
81 name, fn := initServer(t, ctx)
Cosmos Nicolaouae8dd212014-12-13 23:43:08 -080082 defer fn()
83
Jiri Simsa6ac95222015-02-23 16:11:49 -080084 client := v23.GetClient(ctx)
Ankurfb9255a2015-01-21 15:29:38 -080085 call, err := client.StartCall(ctx, name, "Ping", nil)
Cosmos Nicolaouae8dd212014-12-13 23:43:08 -080086 if err != nil {
87 t.Fatalf("unexpected error: %s", err)
88 }
89 response := ""
Todd Wange77f9952015-02-18 13:20:50 -080090 if err := call.Finish(&response); err != nil {
Cosmos Nicolaouae8dd212014-12-13 23:43:08 -080091 t.Fatalf("unexpected error: %s", err)
92 }
93 if got, want := response, "pong"; got != want {
94 t.Fatalf("got %q, want %q", got, want)
95 }
96}
97
98func TestSimpleStreaming(t *testing.T) {
Adam Sadovsky623bed02015-10-30 11:02:43 -070099 ctx, shutdown := test.V23InitWithMounttable()
Ankurfb9255a2015-01-21 15:29:38 -0800100 defer shutdown()
101 name, fn := initServer(t, ctx)
Cosmos Nicolaouae8dd212014-12-13 23:43:08 -0800102 defer fn()
103
Cosmos Nicolaouae8dd212014-12-13 23:43:08 -0800104 inc := 1
Jiri Simsa6ac95222015-02-23 16:11:49 -0800105 call, err := v23.GetClient(ctx).StartCall(ctx, name, "Inc", []interface{}{inc})
Cosmos Nicolaouae8dd212014-12-13 23:43:08 -0800106 if err != nil {
107 t.Fatalf("unexpected error: %s", err)
108 }
109
110 want := 10
111 for i := 0; i <= want; i++ {
112 if err := call.Send(i); err != nil {
113 t.Fatalf("unexpected error: %s", err)
114 }
115 got := -1
116 if err = call.Recv(&got); err != nil {
117 t.Fatalf("unexpected error: %s", err)
118 }
119 if want := i + inc; got != want {
Asim Shankara036a0f2015-05-08 11:22:54 -0700120 t.Fatalf("got %d, want %d", got, want)
Cosmos Nicolaouae8dd212014-12-13 23:43:08 -0800121 }
122 }
123 call.CloseSend()
124 final := -1
Todd Wange77f9952015-02-18 13:20:50 -0800125 err = call.Finish(&final)
126 if err != nil {
127 t.Errorf("unexpected error: %#v", err)
Cosmos Nicolaouae8dd212014-12-13 23:43:08 -0800128 }
Cosmos Nicolaouae8dd212014-12-13 23:43:08 -0800129 if got := final; got != want {
Todd Wange77f9952015-02-18 13:20:50 -0800130 t.Fatalf("got %d, want %d", got, want)
Cosmos Nicolaouae8dd212014-12-13 23:43:08 -0800131 }
132}