Repository navigation
Expand file tree
/
Copy pathgrpc_client.go
More file actions
72 lines (62 loc) · 2.12 KB
/
Copy pathgrpc_client.go
File metadata and controls
72 lines (62 loc) · 2.12 KB
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
package central
import (
"github.com/stackrox/rox/pkg/concurrency"
"github.com/stackrox/rox/pkg/grpc/util"
"github.com/stackrox/rox/pkg/sync"
"github.com/stackrox/rox/sensor/common/centralclient"
"google.golang.org/grpc"
"google.golang.org/grpc/connectivity"
)
type fakeGRPCClient struct {
okSig concurrency.Signal
stopSig concurrency.ErrorSignal
conn *grpc.ClientConn
connMtx *sync.Mutex
}
// FakeGRPCFactory implements centralclient.CentralConnectionFactory interface and additional functions for testing
// purposes only.
type FakeGRPCFactory interface {
centralclient.CentralConnectionFactory
OverwriteCentralConnection(newConn *grpc.ClientConn)
}
// MakeFakeConnectionFactory creates the fake gRPC client object given a gRPC connection.
func MakeFakeConnectionFactory(c *grpc.ClientConn) *fakeGRPCClient {
return &fakeGRPCClient{
conn: c,
stopSig: concurrency.NewErrorSignal(),
okSig: concurrency.NewSignal(),
connMtx: &sync.Mutex{},
}
}
func (f *fakeGRPCClient) OverwriteCentralConnection(newConn *grpc.ClientConn) {
f.connMtx.Lock()
defer f.connMtx.Unlock()
f.conn = newConn
}
// SetCentralConnectionWithRetries is the implementation of the concurrent function SetCentralConnectionWithRetries
// that sensor uses to set the gRPC connection to all its components. Present test version simply.
func (f *fakeGRPCClient) SetCentralConnectionWithRetries(_ centralclient.ClusterIDPeekWriter, ptr *util.LazyClientConn, _ centralclient.CertLoader) {
f.connMtx.Lock()
defer f.connMtx.Unlock()
ptr.Set(f.conn)
if f.conn.GetState() == connectivity.Ready {
f.okSig.Signal()
} else {
f.stopSig.Signal()
}
}
// StopSignal returns a signal that is sent if there is an error.
// This signal is never called.
func (f *fakeGRPCClient) StopSignal() concurrency.ReadOnlyErrorSignal {
return &f.stopSig
}
// OkSignal returns a signal that is sent if connection was swapped.
// This signal is triggered instantly on calling SetCentralConnectionWithRetries.
func (f *fakeGRPCClient) OkSignal() concurrency.ReadOnlySignal {
return &f.okSig
}
// Reset signals
func (f *fakeGRPCClient) Reset() {
f.stopSig.Reset()
f.okSig.Reset()
}