-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplicator_test.go
135 lines (109 loc) · 3.29 KB
/
replicator_test.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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package sofa
import (
"fmt"
"testing"
"time"
"github.com/nbio/st"
"github.com/h2non/gock"
)
var (
ReplicatorTestDB = "replicator_test"
ReplicatorTestTargetDB = "replicator_test_target"
ReplicatorTestName = "my_repl"
)
func TestReplicatorSave(t *testing.T) {
defer gock.Off()
gock.New(fmt.Sprintf("https://%s", globalTestConnections.Version1MockHost)).
Put("/_replicator/_myrepl").
Reply(201).
JSON(map[string]interface{}{
"ok": true,
"id": "_myrepl",
"rev": "1-801609c9fdb4c6d196820c5b1f3c26c9",
}).
SetHeader("Etag", `"1-801609c9fdb4c6d196820c5b1f3c26c9"`)
con := globalTestConnections.Version1(t, true)
repl := NewReplication("_myrepl", "mydb", "yourdb")
repl.CreateTarget = true
repl.Continuous = true
rev, err := con.PutReplication(&repl)
st.Assert(t, err, nil)
st.Assert(t, rev, "1-801609c9fdb4c6d196820c5b1f3c26c9")
}
func TestReplicatorReal(t *testing.T) {
con := globalTestConnections.Version1(t, false)
// Run the cleanup as a best effort task - failures here do not case the test to fail
defer func() {
if err := con.DeleteDatabase(ReplicatorTestDB); err != nil {
t.Logf("error cleaning up source DB: %s", err)
}
if err := con.DeleteDatabase(ReplicatorTestTargetDB); err != nil {
t.Logf("error cleaning up target DB: %s", err)
}
}()
// Create a new database
_, err := con.CreateDatabase(ReplicatorTestDB)
if err != nil {
t.Fatal(err)
}
getRepl, err := con.Replication(ReplicatorTestName, "")
if err != nil {
if !ErrorStatus(err, 404) {
t.Fatal(err)
}
} else {
_, err := con.DeleteReplication(&getRepl)
if err != nil {
t.Fatal(err)
}
}
putRepl := NewReplication(ReplicatorTestName, ReplicatorTestDB, ReplicatorTestTargetDB)
putRepl.CreateTarget = true
putRepl.Continuous = true
putRepl.Owner = "admin"
putRepl.UserContext = map[string]interface{}{"roles": []string{"_admin"}}
_, err = con.PutReplication(&putRepl)
st.Assert(t, err, nil)
// This loop handles the case where CouchDB is slow to create the database after the replication
// is created. This seems to get slower after the first time the replication is created but the
// race condition is always present.
getDBAttempts := 1
for {
_, err = con.EnsureDatabase(ReplicatorTestTargetDB)
if err != nil {
if getDBAttempts >= 10 {
fmt.Println(time.Now())
t.Fatalf("couldn't get the created database after %d attempts: %s", getDBAttempts, err)
}
time.Sleep(1 * time.Second)
getDBAttempts++
continue
}
break
}
// This loop is to handle the case where we were able to retrieve the DB before CouchDB had updated
// the replicator document to account for this - in this case the replicator document can have the state
// set to error with the reason: "db_not_found: could not open replicator_test_target"
getReplAttempts := 1
for {
getRepl, err = con.Replication(ReplicatorTestName, "")
st.Assert(t, err, nil)
if getRepl.ReplicationState != "triggered" {
if getReplAttempts >= 3 {
fmt.Println(time.Now())
t.Fatalf(
"replication still in state '%s' after %d attempts: %s",
getRepl.ReplicationState,
getReplAttempts,
getRepl.ReplicationStateReason,
)
}
time.Sleep(1 * time.Second)
getReplAttempts++
continue
}
break
}
_, err = con.DeleteReplication(&getRepl)
st.Assert(t, err, nil)
}