github.com/turingchain2020/turingchain@v1.1.21/blockchain/update_test.go (about) 1 // Copyright Turing Corp. 2018 All Rights Reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package blockchain 6 7 import ( 8 "errors" 9 "testing" 10 11 "github.com/turingchain2020/turingchain/types" 12 "github.com/stretchr/testify/assert" 13 "github.com/stretchr/testify/mock" 14 15 client "github.com/turingchain2020/turingchain/queue" 16 clientMocks "github.com/turingchain2020/turingchain/queue/mocks" 17 ) 18 19 func TestUpgradePlugin(t *testing.T) { 20 cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring()) 21 22 cli := new(clientMocks.Client) 23 cli.On("Sub", "blockchain").Return(nil) 24 cli.On("GetConfig").Return(cfg) 25 cli.On("Recv").Return(nil) 26 msg := &client.Message{Topic: "execs"} 27 errSend := errors.New("Send error") 28 errWait := errors.New("Wait error") 29 30 cases := []struct { 31 m1 *client.Message 32 send error 33 wait error 34 resp *client.Message 35 }{ 36 {msg, nil, nil, nil}, 37 {msg, nil, nil, &client.Message{Data: &types.LocalDBSet{}}}, 38 {msg, nil, nil, &client.Message{Data: &types.LocalDBSet{KV: []*types.KeyValue{}}}}, 39 {msg, errSend, nil, nil}, 40 {msg, nil, errWait, nil}, 41 } 42 43 for _, c := range cases { 44 func() { 45 c1 := c 46 defer func() { 47 err := recover() 48 var expat error 49 if c1.send != nil { 50 expat = c1.send 51 } else if c1.wait != nil { 52 expat = c1.wait 53 } 54 if err == nil { 55 assert.Nil(t, expat) 56 } else { 57 e1 := err.(error) 58 assert.Equal(t, expat.Error(), e1.Error()) 59 } 60 }() 61 62 cli.On("NewMessage", "execs", int64(types.EventUpgrade), nil).Return(msg).Once() 63 cli.On("Send", msg, true).Return(c1.send).Once() 64 cli.On("SendTimeout", mock.Anything, mock.Anything, mock.Anything).Return(c1.send).Once() 65 if c1.send == nil { 66 cli.On("Wait", msg).Return(c1.resp, c1.wait).Once() 67 } 68 chain := New(cfg) 69 chain.client = cli 70 chain.UpgradePlugin() 71 }() 72 } 73 }