-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
99 lines (79 loc) · 3.13 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
90
91
92
93
94
95
96
97
98
99
package goroom
import (
"fmt"
"testing"
"github.com/adonmo/goroom/room/mocks"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)
type RoomInitialzationTestSuite struct {
suite.Suite
MockCtrl *gomock.Controller
Initializer *mocks.MockInitializer
}
func (s *RoomInitialzationTestSuite) SetupTest() {
s.MockCtrl = gomock.NewController(s.T())
s.Initializer = mocks.NewMockInitializer(s.MockCtrl)
}
func (s *RoomInitialzationTestSuite) TestInitializeRoomWithIdentityCalculationError() {
expectedError := fmt.Errorf("Some hashing error")
s.Initializer.EXPECT().CalculateIdentityHash().Return("", expectedError).Times(2)
//With Fallback Enabled
assert.Equal(s.T(), expectedError, InitializeRoom(s.Initializer, true))
//Without Fallback Enabled
assert.Equal(s.T(), expectedError, InitializeRoom(s.Initializer, false))
}
func (s *RoomInitialzationTestSuite) TestInitializeRoomWithErrorOnFirstInit() {
identityHash := "asasasawfw"
initError := fmt.Errorf("Error during initialization")
//With Retry not Recommended
gomock.InOrder(
s.Initializer.EXPECT().CalculateIdentityHash().Return(identityHash, nil),
s.Initializer.EXPECT().Init(identityHash).Return(false, initError),
)
//With Fallback Enabled
assert.Equal(s.T(), initError, InitializeRoom(s.Initializer, true))
//With Retry not Recommended
gomock.InOrder(
s.Initializer.EXPECT().CalculateIdentityHash().Return(identityHash, nil),
s.Initializer.EXPECT().Init(identityHash).Return(false, initError),
)
//With Fallback not Enabled
assert.Equal(s.T(), initError, InitializeRoom(s.Initializer, true))
//With Retry Recommended and Clean up success
gomock.InOrder(
s.Initializer.EXPECT().CalculateIdentityHash().Return(identityHash, nil),
s.Initializer.EXPECT().Init(identityHash).Return(true, initError),
s.Initializer.EXPECT().PerformDBCleanUp().Return(nil),
s.Initializer.EXPECT().Init(identityHash).Return(true, nil),
)
//With Fallback Enabled
assert.Equal(s.T(), nil, InitializeRoom(s.Initializer, true))
//With Retry Recommended and Clean up success
gomock.InOrder(
s.Initializer.EXPECT().CalculateIdentityHash().Return(identityHash, nil),
s.Initializer.EXPECT().Init(identityHash).Return(false, initError),
)
//With Fallback not Enabled
assert.Equal(s.T(), initError, InitializeRoom(s.Initializer, true))
dbCleanUpError := fmt.Errorf("Error in DB Cleanup")
//With Retry Recommended and Clean up error
gomock.InOrder(
s.Initializer.EXPECT().CalculateIdentityHash().Return(identityHash, nil),
s.Initializer.EXPECT().Init(identityHash).Return(true, initError),
s.Initializer.EXPECT().PerformDBCleanUp().Return(dbCleanUpError),
)
//With Fallback Enabled
assert.Equal(s.T(), dbCleanUpError, InitializeRoom(s.Initializer, true))
//With Retry Recommended and Clean up error
gomock.InOrder(
s.Initializer.EXPECT().CalculateIdentityHash().Return(identityHash, nil),
s.Initializer.EXPECT().Init(identityHash).Return(false, initError),
)
//With Fallback not Enabled
assert.Equal(s.T(), initError, InitializeRoom(s.Initializer, true))
}
func TestMain(t *testing.T) {
suite.Run(t, new(RoomInitialzationTestSuite))
}