forked from TheThingsNetwork/lorawan-stack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcluster_test.go
More file actions
104 lines (87 loc) · 2.76 KB
/
cluster_test.go
File metadata and controls
104 lines (87 loc) · 2.76 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright © 2019 The Things Network Foundation, The Things Industries B.V.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package component_test
import (
"context"
"net"
"testing"
"time"
"github.com/smartystreets/assertions"
"go.thethings.network/lorawan-stack/v3/pkg/cluster"
"go.thethings.network/lorawan-stack/v3/pkg/component"
"go.thethings.network/lorawan-stack/v3/pkg/config"
"go.thethings.network/lorawan-stack/v3/pkg/rpcserver"
"go.thethings.network/lorawan-stack/v3/pkg/ttnpb"
"go.thethings.network/lorawan-stack/v3/pkg/util/test"
"go.thethings.network/lorawan-stack/v3/pkg/util/test/assertions/should"
)
func TestPeers(t *testing.T) {
a := assertions.New(t)
lis, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
panic(err)
}
defer lis.Close()
ctx, cancel := context.WithCancel(test.Context())
defer cancel()
srv := rpcserver.New(ctx)
go srv.Serve(lis)
defer srv.Stop()
var c *component.Component
config := &component.Config{
ServiceBase: config.ServiceBase{Cluster: cluster.Config{
Name: "test-cluster",
NetworkServer: lis.Addr().String(),
TLS: false,
}},
}
c, err = component.New(test.GetLogger(t), config)
a.So(err, should.BeNil)
err = c.Start()
a.So(err, should.BeNil)
unusedRoles := []ttnpb.ClusterRole{
ttnpb.ClusterRole_APPLICATION_SERVER,
ttnpb.ClusterRole_GATEWAY_SERVER,
ttnpb.ClusterRole_JOIN_SERVER,
ttnpb.ClusterRole_ACCESS,
ttnpb.ClusterRole_ENTITY_REGISTRY,
}
var peer cluster.Peer
for i := 0; i < 20; i++ {
time.Sleep(20 * time.Millisecond) // Wait for peers to join cluster.
peer, err = c.GetPeer(context.Background(), ttnpb.ClusterRole_NETWORK_SERVER, nil)
if err == nil {
break
}
}
if !a.So(peer, should.NotBeNil) {
t.FailNow()
}
conn, err := peer.Conn()
a.So(err, should.BeNil)
a.So(conn, should.NotBeNil)
for _, role := range unusedRoles {
peer, err := c.GetPeer(context.Background(), role, nil)
a.So(peer, should.BeNil)
a.So(err, should.NotBeNil)
}
peers, err := c.GetPeers(context.Background(), ttnpb.ClusterRole_NETWORK_SERVER)
a.So(peers, should.HaveLength, 1)
a.So(err, should.BeNil)
for _, role := range unusedRoles {
peers, err = c.GetPeers(context.Background(), role)
a.So(peers, should.HaveLength, 0)
a.So(err, should.BeNil)
}
}