From 6ee477b31729529413c83483dbe707f32026b88a Mon Sep 17 00:00:00 2001 From: rakshasa Date: Mon, 19 Feb 2024 14:28:02 +0100 Subject: [PATCH] fix: changing entry conversations in mock connection manager caused race conditions --- internal/test/ouroboros_mock/connection.go | 9 ++++++--- protocol/message.go | 4 ++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/internal/test/ouroboros_mock/connection.go b/internal/test/ouroboros_mock/connection.go index 2885241b..a769410b 100644 --- a/internal/test/ouroboros_mock/connection.go +++ b/internal/test/ouroboros_mock/connection.go @@ -187,9 +187,12 @@ func (c *Connection) processInputEntry(entry ConversationEntry) error { if msg == nil { return fmt.Errorf("received unknown message type: %d", msgType) } - // Set CBOR for expected message to match received to make comparison easier - entry.InputMessage.SetCbor(msg.Cbor()) - // Compare received message to expected message + + // Compare received message to expected message, excluding the cbor content + // + // As changing the CBOR of the expected message is not thread-safe, we instead clear the + // CBOR of the received message + msg.SetCbor(nil) if !reflect.DeepEqual(msg, entry.InputMessage) { return fmt.Errorf( "parsed message does not match expected value: got %#v, expected %#v", diff --git a/protocol/message.go b/protocol/message.go index 0948372c..875a9a29 100644 --- a/protocol/message.go +++ b/protocol/message.go @@ -31,6 +31,10 @@ type MessageBase struct { // SetCbor stores the original CBOR that was parsed func (m *MessageBase) SetCbor(data []byte) { + if data == nil { + m.rawCbor = nil + return + } m.rawCbor = make([]byte, len(data)) copy(m.rawCbor, data) }