github.com/kaleido-io/firefly@v0.0.0-20210622132723-8b4b6aacb971/internal/events/transaction_update_test.go (about) 1 // Copyright © 2021 Kaleido, Inc. 2 // 3 // SPDX-License-Identifier: Apache-2.0 4 // 5 // Licensed under the Apache License, Version 2.0 (the "License"); 6 // you may not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 17 package events 18 19 import ( 20 "fmt" 21 "testing" 22 "time" 23 24 "github.com/kaleido-io/firefly/internal/retry" 25 "github.com/kaleido-io/firefly/mocks/blockchainmocks" 26 "github.com/kaleido-io/firefly/mocks/databasemocks" 27 "github.com/kaleido-io/firefly/pkg/fftypes" 28 "github.com/stretchr/testify/assert" 29 "github.com/stretchr/testify/mock" 30 ) 31 32 func TestTxSubmissionUpdateRetryThenFound(t *testing.T) { 33 em, cancel := newTestEventManager(t) 34 defer cancel() 35 mdi := em.database.(*databasemocks.Plugin) 36 mbi := &blockchainmocks.Plugin{} 37 em.retry = retry.Retry{ 38 InitialDelay: 1 * time.Microsecond, 39 MaximumDelay: 1 * time.Microsecond, 40 } 41 em.opCorrelationRetries = 2 42 43 opID := fftypes.NewUUID() 44 mbi.On("Name").Return("ut") 45 mdi.On("GetOperations", em.ctx, mock.Anything).Return(nil, fmt.Errorf("will retry")).Once() 46 mdi.On("GetOperations", em.ctx, mock.Anything).Return(nil, nil).Once() // retry again 47 mdi.On("GetOperations", em.ctx, mock.Anything).Return([]*fftypes.Operation{ 48 {ID: opID}, 49 }, nil) 50 mdi.On("UpdateOperation", em.ctx, uuidMatches(opID), mock.Anything).Return(nil) 51 52 info := fftypes.JSONObject{"some": "info"} 53 err := em.TxSubmissionUpdate(mbi, "tracking12345", fftypes.OpStatusFailed, "tx12345", "some error", info) 54 assert.NoError(t, err) 55 } 56 57 func TestTransactionLookupNotFound(t *testing.T) { 58 em, cancel := newTestEventManager(t) 59 defer cancel() 60 mdi := em.database.(*databasemocks.Plugin) 61 mbi := &blockchainmocks.Plugin{} 62 em.opCorrelationRetries = 0 63 64 mbi.On("Name").Return("ut") 65 mdi.On("GetOperations", em.ctx, mock.Anything).Return(nil, fmt.Errorf("pop")).Once() 66 67 info := fftypes.JSONObject{"some": "info"} 68 err := em.TxSubmissionUpdate(mbi, "tracking12345", fftypes.OpStatusFailed, "tx12345", "some error", info) 69 assert.NoError(t, err) // swallowed after logging 70 } 71 72 func TestTxSubmissionUpdateError(t *testing.T) { 73 em, cancel := newTestEventManager(t) 74 defer cancel() 75 mdi := em.database.(*databasemocks.Plugin) 76 mbi := &blockchainmocks.Plugin{} 77 em.opCorrelationRetries = 0 78 79 opID := fftypes.NewUUID() 80 mbi.On("Name").Return("ut") 81 mdi.On("GetOperations", em.ctx, mock.Anything).Return([]*fftypes.Operation{ 82 {ID: opID}, 83 }, nil) 84 mdi.On("UpdateOperation", em.ctx, uuidMatches(opID), mock.Anything).Return(fmt.Errorf("pop")) 85 86 info := fftypes.JSONObject{"some": "info"} 87 err := em.TxSubmissionUpdate(mbi, "tracking12345", fftypes.OpStatusFailed, "tx12345", "some error", info) 88 assert.EqualError(t, err, "pop") 89 }