github.com/m3db/m3@v1.5.0/src/dbnode/client/client_test.go (about) 1 // Copyright (c) 2016 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package client 22 23 import ( 24 "errors" 25 "fmt" 26 "sync" 27 "testing" 28 29 "github.com/golang/mock/gomock" 30 "github.com/stretchr/testify/assert" 31 ) 32 33 func testClient(t *testing.T, ctrl *gomock.Controller) Client { 34 opts := NewMockOptions(ctrl) 35 opts.EXPECT().Validate().Return(nil) 36 37 client, err := NewClient(opts) 38 assert.NoError(t, err) 39 assert.NotNil(t, client) 40 41 return client 42 } 43 44 func TestClientNewClientValidatesOptions(t *testing.T) { 45 ctrl := gomock.NewController(t) 46 defer ctrl.Finish() 47 48 testClient(t, ctrl) 49 50 anError := fmt.Errorf("an error") 51 opts := NewMockOptions(ctrl) 52 opts.EXPECT().Validate().Return(anError) 53 54 _, err := NewClient(opts) 55 assert.Error(t, err) 56 assert.Equal(t, anError, err) 57 } 58 59 func TestClientNewSessionOpensSession(t *testing.T) { 60 ctrl := gomock.NewController(t) 61 defer ctrl.Finish() 62 63 var mockSession Session 64 client := testClient(t, ctrl).(*client) 65 client.newSessionFn = func(opts Options, _ []Options, _ ...replicatedSessionOption) (clientSession, error) { 66 session := NewMockclientSession(ctrl) 67 session.EXPECT().Open().Return(nil) 68 mockSession = session 69 return session, nil 70 } 71 72 session, err := client.NewSession() 73 assert.NoError(t, err) 74 assert.Equal(t, mockSession, session) 75 } 76 77 func TestClientNewSessionFailCreateReturnsError(t *testing.T) { 78 ctrl := gomock.NewController(t) 79 defer ctrl.Finish() 80 81 client := testClient(t, ctrl).(*client) 82 anError := fmt.Errorf("an error") 83 client.newSessionFn = func(opts Options, _ []Options, _ ...replicatedSessionOption) (clientSession, error) { 84 return nil, anError 85 } 86 87 session, err := client.NewSession() 88 assert.Error(t, err) 89 assert.Equal(t, anError, err) 90 assert.Nil(t, session) 91 } 92 93 func TestClientNewSessionFailOpenReturnsError(t *testing.T) { 94 ctrl := gomock.NewController(t) 95 defer ctrl.Finish() 96 97 client := testClient(t, ctrl).(*client) 98 anError := fmt.Errorf("an error") 99 client.newSessionFn = func(opts Options, _ []Options, _ ...replicatedSessionOption) (clientSession, error) { 100 session := NewMockclientSession(ctrl) 101 session.EXPECT().Open().Return(anError) 102 return session, nil 103 } 104 105 session, err := client.NewSession() 106 assert.Error(t, err) 107 assert.Equal(t, anError, err) 108 assert.Nil(t, session) 109 } 110 111 func TestClientDefaultSessionAlreadyCreated(t *testing.T) { 112 ctrl := gomock.NewController(t) 113 defer ctrl.Finish() 114 115 client := testClient(t, ctrl).(*client) 116 session := NewMockAdminSession(ctrl) 117 client.session = session 118 119 defaultSession, err := client.DefaultSession() 120 assert.NoError(t, err) 121 assert.Equal(t, session, defaultSession) 122 } 123 124 func TestClientDefaultSessionNotCreatedNoError(t *testing.T) { 125 ctrl := gomock.NewController(t) 126 defer ctrl.Finish() 127 128 client := testClient(t, ctrl).(*client) 129 session := NewMockclientSession(ctrl) 130 session.EXPECT().Open().Return(nil) 131 client.newSessionFn = func(opts Options, _ []Options, _ ...replicatedSessionOption) (clientSession, error) { 132 return session, nil 133 } 134 135 defaultSession, err := client.DefaultSession() 136 assert.NoError(t, err) 137 assert.Equal(t, session, defaultSession) 138 } 139 140 func TestClientDefaultSessionNotCreatedWithError(t *testing.T) { 141 ctrl := gomock.NewController(t) 142 defer ctrl.Finish() 143 144 client := testClient(t, ctrl).(*client) 145 expectedErr := errors.New("foo") 146 client.newSessionFn = func(opts Options, _ []Options, _ ...replicatedSessionOption) (clientSession, error) { 147 return nil, expectedErr 148 } 149 150 defaultSession, err := client.DefaultSession() 151 assert.Equal(t, expectedErr, err) 152 assert.Nil(t, defaultSession) 153 } 154 155 func TestClientDefaultSessionMultipleSimultaneousRequests(t *testing.T) { 156 ctrl := gomock.NewController(t) 157 defer ctrl.Finish() 158 159 var ( 160 lock sync.Mutex 161 defaultSession AdminSession 162 wg sync.WaitGroup 163 ) 164 165 client := testClient(t, ctrl).(*client) 166 client.newSessionFn = func(opts Options, _ []Options, _ ...replicatedSessionOption) (clientSession, error) { 167 session := NewMockclientSession(ctrl) 168 session.EXPECT().Open().Return(nil) 169 lock.Lock() 170 if defaultSession == nil { 171 defaultSession = session 172 } 173 lock.Unlock() 174 return session, nil 175 } 176 177 numRequests := 10 178 for i := 0; i < numRequests; i++ { 179 wg.Add(1) 180 go func() { 181 defer wg.Done() 182 sess, err := client.DefaultSession() 183 assert.NoError(t, err) 184 assert.Equal(t, defaultSession, sess) 185 }() 186 } 187 188 wg.Wait() 189 }