forked from lileio/lile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
68 lines (55 loc) · 1.49 KB
/
utils.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
package lile
import (
"net"
"time"
"github.com/gofrs/uuid"
"google.golang.org/grpc"
)
// NewTestServer is a helper function to create a gRPC server on a non-network
// socket and it returns the socket location and a func to call which starts
// the server
func NewTestServer(s *grpc.Server) (string, func()) {
socketAddress, listener, err := getTestServerTransport()
if err != nil {
panic(err)
}
return socketAddress, func() {
s.Serve(listener)
}
}
// TestConn is a connection that connects to a socket based connection
func TestConn(addr string) *grpc.ClientConn {
conn, err := grpc.Dial(
addr,
grpc.WithDialer(func(addr string, d time.Duration) (net.Conn, error) {
return dialTestServer(addr)
}),
grpc.WithInsecure(),
grpc.WithTimeout(1*time.Second),
grpc.WithBlock(),
)
if err != nil {
panic(err)
}
return conn
}
// Creates a server listener dependent on the underlying platform. Windows
// hosts will have a Windows Named pipe, anything else gets a UNIX socket
func getTestServerTransport() (string, net.Listener, error) {
var uniqueAddress string
// Create a random string for part of the address
uid, err := uuid.NewV1()
if err != nil {
return "", nil, err
}
uniqueAddress = formatPlatformTestSeverAddress(uid.String())
serverListener, err := getTestServerListener(uniqueAddress)
if err != nil {
return "", nil, err
}
return uniqueAddress, serverListener, nil
}
func generateID(n string) string {
uid, _ := uuid.NewV4()
return n + "-" + uid.String()
}