Skip to content

Commit 2dffcda

Browse files
committed
test(backend, transport): add integration test for handleConversation
1 parent b2a7055 commit 2dffcda

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

backend/pkg/transport/transport_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"time"
1515

1616
"github.com/HyperloopUPV-H8/h9-backend/pkg/abstraction"
17+
"github.com/HyperloopUPV-H8/h9-backend/pkg/transport/network"
1718
"github.com/HyperloopUPV-H8/h9-backend/pkg/transport/network/sniffer"
1819
"github.com/HyperloopUPV-H8/h9-backend/pkg/transport/network/tcp"
1920
"github.com/HyperloopUPV-H8/h9-backend/pkg/transport/network/tftp"
@@ -1106,6 +1107,36 @@ func TestHandleSniffer_Dispatches(t *testing.T) {
11061107
_ = api
11071108
}
11081109

1110+
func TestHandleConversation_DispatchesAndStopsOnError(t *testing.T) {
1111+
tr, api := createTestTransport(t)
1112+
1113+
pkt := data.NewPacket(100)
1114+
pkt.SetTimestamp(time.Unix(0, 0))
1115+
buf, err := tr.encoder.Encode(pkt)
1116+
if err != nil {
1117+
t.Fatalf("encode failed: %v", err)
1118+
}
1119+
defer tr.encoder.ReleaseBuffer(buf)
1120+
1121+
socket := network.Socket{
1122+
SrcIP: "127.0.0.1",
1123+
SrcPort: 8000,
1124+
DstIP: "127.0.0.1",
1125+
DstPort: 8001,
1126+
}
1127+
1128+
reader := bytes.NewReader(buf.Bytes())
1129+
tr.handleConversation(socket, reader)
1130+
1131+
if err := waitForCondition(func() bool { return len(api.GetNotifications()) >= 1 }, time.Second, "packet notification"); err != nil {
1132+
t.Fatal(err)
1133+
}
1134+
// After the first packet, DecodeNext will hit EOF and SendFault will result in an error notification.
1135+
if err := waitForCondition(func() bool { return len(api.GetNotifications()) >= 2 }, 2*time.Second, "error notification"); err != nil {
1136+
t.Fatal(err)
1137+
}
1138+
}
1139+
11091140
// Helper function to mimic errors.As behavior
11101141
func ErrorAs(err error, target interface{}) bool {
11111142
switch target := target.(type) {

0 commit comments

Comments
 (0)