Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions internal/blockchain/ethereum/ethereum.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ func (e *Ethereum) handleMessageBatch(ctx context.Context, messages []interface{
}

func (e *Ethereum) eventLoop() {
defer e.wsconn.Close()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks correct to me. I'm now wondering what is the purpose of close(e.closed) on the line above this? Perhaps that's a @peterbroadhurst question?

Also, I assume we need this same fix in the Fabric plugin?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The e.closed is to account for Unit Tests waiting for the event loop to end before exiting, which is important otherwise you can get spill-over of execution after the end of tests, calling mocks after they are cancelled.

It does feel like the e.wsconn.Close() should come before the close(e.closed) to me (think the risk of spurious UT failures is low, but I couldn't rule them out)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I assume we need this same fix in the Fabric plugin?

Agreed. Think we should get the close into both - sorry it's missing, surprised at that and how it hasn't caused problems before 🤷

defer close(e.closed)
l := log.L(e.ctx).WithField("role", "event-loop")
ctx := log.WithLogger(e.ctx, l)
Expand Down
4 changes: 4 additions & 0 deletions internal/blockchain/ethereum/ethereum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,7 @@ func TestEventLoopContextCancelled(t *testing.T) {
r := make(<-chan []byte)
wsm := e.wsconn.(*wsmocks.WSClient)
wsm.On("Receive").Return(r)
wsm.On("Close").Return()
e.closed = make(chan struct{})
e.eventLoop() // we're simply looking for it exiting
}
Expand All @@ -815,6 +816,7 @@ func TestEventLoopReceiveClosed(t *testing.T) {
wsm := e.wsconn.(*wsmocks.WSClient)
close(r)
wsm.On("Receive").Return((<-chan []byte)(r))
wsm.On("Close").Return()
e.closed = make(chan struct{})
e.eventLoop() // we're simply looking for it exiting
}
Expand All @@ -827,6 +829,7 @@ func TestEventLoopSendClosed(t *testing.T) {
close(r)
wsm.On("Receive").Return((<-chan []byte)(r))
wsm.On("Send", mock.Anything, mock.Anything).Return(fmt.Errorf("pop"))
wsm.On("Close").Return()
e.closed = make(chan struct{})
e.eventLoop() // we're simply looking for it exiting
}
Expand Down Expand Up @@ -887,6 +890,7 @@ func TestHandleBadPayloadsAndThenReceiptFailure(t *testing.T) {
e.closed = make(chan struct{})

wsm.On("Receive").Return((<-chan []byte)(r))
wsm.On("Close").Return()
operationID := fftypes.NewUUID()
data := []byte(`{
"_id": "6fb94fff-81d3-4094-567d-e031b1871694",
Expand Down
1 change: 1 addition & 0 deletions internal/blockchain/fabric/fabric.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ func (f *Fabric) handleMessageBatch(ctx context.Context, messages []interface{})
}

func (f *Fabric) eventLoop() {
defer f.wsconn.Close()
defer close(f.closed)
l := log.L(f.ctx).WithField("role", "event-loop")
ctx := log.WithLogger(f.ctx, l)
Expand Down
4 changes: 4 additions & 0 deletions internal/blockchain/fabric/fabric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,7 @@ func TestEventLoopContextCancelled(t *testing.T) {
r := make(<-chan []byte)
wsm := e.wsconn.(*wsmocks.WSClient)
wsm.On("Receive").Return(r)
wsm.On("Close").Return()
e.closed = make(chan struct{})
e.eventLoop() // we're simply looking for it exiting
}
Expand All @@ -809,6 +810,7 @@ func TestEventLoopReceiveClosed(t *testing.T) {
wsm := e.wsconn.(*wsmocks.WSClient)
close(r)
wsm.On("Receive").Return((<-chan []byte)(r))
wsm.On("Close").Return()
e.closed = make(chan struct{})
e.eventLoop() // we're simply looking for it exiting
}
Expand All @@ -820,6 +822,7 @@ func TestEventLoopSendClosed(t *testing.T) {
wsm := e.wsconn.(*wsmocks.WSClient)
close(r)
wsm.On("Receive").Return((<-chan []byte)(r))
wsm.On("Close").Return()
wsm.On("Send", mock.Anything, mock.Anything).Return(fmt.Errorf("pop"))
e.closed = make(chan struct{})
e.eventLoop() // we're simply looking for it exiting
Expand All @@ -831,6 +834,7 @@ func TestEventLoopUnexpectedMessage(t *testing.T) {
r := make(chan []byte)
wsm := e.wsconn.(*wsmocks.WSClient)
wsm.On("Receive").Return((<-chan []byte)(r))
wsm.On("Close").Return()
e.closed = make(chan struct{})
operationID := fftypes.NewUUID()
data := []byte(`{
Expand Down
2 changes: 1 addition & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ func (c *configPrefix) Array() PrefixArray {
func (c *configPrefixArray) ArraySize() int {
val := viper.Get(c.base)
vt := reflect.TypeOf(val)
if vt != nil && vt.Kind() == reflect.Slice {
if vt != nil && (vt.Kind() == reflect.Slice || vt.Kind() == reflect.Map) {
return reflect.ValueOf(val).Len()
}
return 0
Expand Down
25 changes: 25 additions & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,31 @@ tokens:
assert.Equal(t, []string{"arr1", "arr2"}, sally.GetStringSlice("key2"))
}

func TestMapOfAdminOverridePlugins(t *testing.T) {
defer Reset()

tokPlugins := NewPluginConfig("tokens").Array()
tokPlugins.AddKnownKey("firstkey")
tokPlugins.AddKnownKey("secondkey")
viper.SetConfigType("json")
err := viper.ReadConfig(strings.NewReader(`{
"tokens": {
"0": {
"firstkey": "firstitemfirstkeyvalue",
"secondkey": "firstitemsecondkeyvalue"
},
"1": {
"firstkey": "seconditemfirstkeyvalue",
"secondkey": "seconditemsecondkeyvalue"
}
}
}`))
assert.NoError(t, err)
assert.Equal(t, 2, tokPlugins.ArraySize())
assert.Equal(t, "firstitemfirstkeyvalue", tokPlugins.ArrayEntry(0).Get("firstkey"))
assert.Equal(t, "seconditemsecondkeyvalue", tokPlugins.ArrayEntry(1).Get("secondkey"))
}

func TestGetKnownKeys(t *testing.T) {
knownKeys := GetKnownKeys()
assert.NotEmpty(t, knownKeys)
Expand Down