-
Notifications
You must be signed in to change notification settings - Fork 28
/
impl.go
74 lines (67 loc) · 1.68 KB
/
impl.go
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
// Copyright 2021 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package impl
import (
"fmt"
"os"
"time"
v23 "v.io/v23"
"v.io/v23/context"
"v.io/v23/rpc"
"v.io/x/ref/lib/security/securityflag"
"v.io/x/ref/lib/signals"
"v.io/x/ref/test/compatibility/modules/simple"
)
func RunClient(ctx *context.T, name, msg string, numCalls int) error {
client := simple.SimpleClient(name)
ticker := time.NewTicker(time.Second)
done := make(chan error)
go func() {
defer close(done)
i := 1
for range ticker.C {
ctx, cancel := context.WithTimeout(ctx, time.Second)
before := time.Now()
result, err := client.Ping(ctx, msg)
if err != nil {
done <- fmt.Errorf("%v.%v failed: %v", name, "ping", err)
return
}
fmt.Printf("RESPONSE=%v\n", result)
fmt.Printf("TOOK=%v\n", time.Since(before))
cancel()
if i == numCalls {
return
}
i++
}
}()
select {
case err := <-done:
if err != nil {
fmt.Printf("DONE=%v\n", err)
return err
}
fmt.Printf("DONE=OK\n")
case <-signals.ShutdownOnSignals(ctx):
}
return nil
}
type impl struct{ name string }
func (s *impl) Ping(ctx *context.T, call rpc.ServerCall, msg string) (response string, err error) {
response = s.name + ":" + msg
return
}
func RunServer(ctx *context.T, name string) error {
ctx, server, err := v23.WithNewServer(ctx, name, simple.SimpleServer(&impl{name}), securityflag.NewAuthorizerOrDie(ctx))
if err != nil {
return err
}
fmt.Printf("PID=%d\n", os.Getpid())
for _, ep := range server.Status().Endpoints {
fmt.Printf("NAME=%s\n", ep.Name())
}
<-signals.ShutdownOnSignals(ctx)
return nil
}