-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
89 lines (79 loc) · 1.95 KB
/
main_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
package main
import (
"github.com/Fengxq2014/mars/etcdsrv"
"github.com/gorilla/mux"
"github.com/joho/godotenv"
"github.com/smartystreets/assertions/should"
. "github.com/smartystreets/goconvey/convey"
"log"
"net/http"
"net/http/httptest"
"testing"
)
var ma *mars
func TestMain(m *testing.M) {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
if err != nil {
log.Fatal(err)
}
ma = &mars{
etcdCli: etcdsrv.New().Cli,
}
m.Run()
}
func TestHttpHandler(t *testing.T) {
tt := []struct {
routeVariable string
}{
{"/id"},
}
Convey("TestHttpHandler should return http Status ok, and response body contains snowflake id", t, func() {
for _, tc := range tt {
Convey("Test "+tc.routeVariable, func() {
req, err := http.NewRequest("GET", tc.routeVariable, nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
router := mux.NewRouter()
router.HandleFunc("/id", GetID).Methods("GET")
router.ServeHTTP(rr, req)
So(rr.Code, ShouldEqual, http.StatusOK)
So(len(rr.Body.Bytes()), ShouldEqual, 18)
})
}
})
}
func TestCampaignLeader(t *testing.T) {
Convey(`CampaignLeader first time should return (true, "", nil), second time should return (false, "sss", TxnFailed)`, t, func() {
Convey("Test first time", func() {
b, s, err := ma.campaignLeader()
So(b, ShouldEqual, true)
So(s, ShouldBeEmpty)
So(err, ShouldBeNil)
})
Convey("Test second time", func() {
b, s, err := ma.campaignLeader()
So(b, ShouldEqual, false)
So(s, ShouldNotBeEmpty)
So(err, ShouldEqual, TxnFailed)
})
})
}
func TestGetNode(t *testing.T) {
Convey("GetNode should return a number", t, func() {
Convey("Test first time", func() {
i, err := ma.getNodeNum()
So(err, ShouldBeNil)
So(i, should.BeGreaterThanOrEqualTo, 1)
})
Convey("Test second time", func() {
i, err := ma.getNodeNum()
So(err, ShouldBeNil)
So(i, should.BeGreaterThanOrEqualTo, 2)
})
})
}