Debian Codegraph @

internal/integration/tests/cluster_integration_test.go

raw symbols SCIP
1 // +build cluster
2 // +build integration
3
4 package tests
5
6 import (
7 "time"
8
9 test "gopkg.in/check.v1"
10 r "gopkg.in/rethinkdb/rethinkdb-go.v6"
11 "strconv"
12 "strings"
13 )
14
15 func (s *RethinkSuite) TestClusterDetectNewNode(c *test.C) {
16 h1, p1 := splitAddress(url)
17 h2, p2 := splitAddress(url2)
18 hosts := []r.Host{r.NewHost(h1, p1), r.NewHost(h2, p2)}
19
20 cluster, err := r.NewCluster(hosts, &r.ConnectOpts{
21 Addresses: []string{url, url2},
22 DiscoverHosts: true,
23 NodeRefreshInterval: time.Second,
24 })
25 c.Assert(err, test.IsNil)
26
27 t := time.NewTimer(time.Second * 30)
28 for {
29 select {
30 // Fail if deadline has passed
31 case <-t.C:
32 c.Fatal("No node was added to the cluster")
33 default:
34 // Pass if another node was added
35 if len(cluster.GetNodes()) >= 3 {
36 return
37 }
38 }
39 }
40 }
41
42 func (s *RethinkSuite) TestClusterRecoverAfterNoNodes(c *test.C) {
43 h1, p1 := splitAddress(url)
44 h2, p2 := splitAddress(url2)
45 hosts := []r.Host{r.NewHost(h1, p1), r.NewHost(h2, p2)}
46
47 cluster, err := r.NewCluster(hosts, &r.ConnectOpts{
48 Addresses: []string{url, url2},
49 DiscoverHosts: true,
50 NodeRefreshInterval: time.Second,
51 })
52 c.Assert(err, test.IsNil)
53
54 t := time.NewTimer(time.Second * 30)
55 hasHadZeroNodes := false
56 for {
57 select {
58 // Fail if deadline has passed
59 case <-t.C:
60 c.Fatal("No node was added to the cluster")
61 default:
62 // Check if there are no nodes
63 if len(cluster.GetNodes()) == 0 {
64 hasHadZeroNodes = true
65 }
66
67 // Pass if another node was added
68 if len(cluster.GetNodes()) >= 1 && hasHadZeroNodes {
69 return
70 }
71 }
72 }
73 }
74
75 func (s *RethinkSuite) TestClusterNodeHealth(c *test.C) {
76 session, err := r.Connect(r.ConnectOpts{
77 Addresses: []string{url1, url2, url3},
78 DiscoverHosts: true,
79 NodeRefreshInterval: time.Second,
80 InitialCap: 50,
81 MaxOpen: 200,
82 })
83 c.Assert(err, test.IsNil)
84
85 attempts := 0
86 failed := 0
87 seconds := 0
88
89 t := time.NewTimer(time.Second * 30)
90 tick := time.NewTicker(time.Second)
91 for {
92 select {
93 // Fail if deadline has passed
94 case <-tick.C:
95 seconds++
96 c.Logf("%ds elapsed", seconds)
97 case <-t.C:
98 // Execute queries for 10s and check that at most 5% of the queries fail
99 c.Logf("%d of the %d(%d%%) queries failed", failed, attempts, (failed / attempts))
100 c.Assert(failed <= 100, test.Equals, true)
101 return
102 default:
103 attempts++
104 if err := r.Expr(1).Exec(session); err != nil {
105 c.Logf("Query failed, %s", err)
106 failed++
107 }
108 }
109 }
110 }
111
112 func splitAddress(address string) (hostname string, port int) {
113 hostname = "localhost"
114 port = 28015
115
116 addrParts := strings.Split(address, ":")
117
118 if len(addrParts) >= 1 {
119 hostname = addrParts[0]
120 }
121 if len(addrParts) >= 2 {
122 port, _ = strconv.Atoi(addrParts[1])
123 }
124
125 return
126 }