diff --git a/metadapta_test.go b/metadapta_test.go index 93ed006..d3741ca 100644 --- a/metadapta_test.go +++ b/metadapta_test.go @@ -31,21 +31,23 @@ func TestConnA(test *testing.T) { // server listener, err := net.Listen(network, addr) if err != nil { test.Fatal(err) } - defer listener.Close() + test.Cleanup(func() { listener.Close() }) go func() { test.Log("SERVER listening") conn, err := listener.Accept() if err != nil { test.Error("SERVER", err); return } defer conn.Close() + test.Cleanup(func() { conn.Close() }) a := AdaptA(conn, ServerSide) trans, err := a.OpenTrans() if err != nil { test.Error("SERVER", err); return } - defer trans.Close() + test.Cleanup(func() { trans.Close() }) for method, payload := range payloads { - test.Log("SERVER", method, payload) + test.Log("SERVER m:", method, "p:", payload) err := trans.Send(uint16(method), []byte(payload)) if err != nil { test.Error("SERVER", err); return } } + test.Log("SERVER closing connection") }() // client @@ -54,18 +56,18 @@ func TestConnA(test *testing.T) { if err != nil { test.Fatal("CLIENT", err) } test.Log("CLIENT dialed") a := AdaptA(conn, ClientSide) - defer a.Close() + test.Cleanup(func() { a.Close() }) test.Log("CLIENT accepting transaction") trans, err := a.AcceptTrans() if err != nil { test.Fatal("CLIENT", err) } test.Log("CLIENT accepted transaction") - defer trans.Close() + test.Cleanup(func() { trans.Close() }) for method, payload := range payloads { test.Log("CLIENT waiting...") gotMethod, gotPayloadBytes, err := trans.Receive() if err != nil { test.Fatal("CLIENT", err) } gotPayload := string(gotPayloadBytes) - test.Log("CLIENT", gotMethod, gotPayload) + test.Log("CLIENT m:", gotMethod, "p:", gotPayload) if int(gotMethod) != method { test.Errorf("CLIENT method not equal") } @@ -73,11 +75,13 @@ func TestConnA(test *testing.T) { test.Errorf("CLIENT payload not equal") } } + test.Log("CLIENT waiting for connection close...") _, _, err = trans.Receive() if !errors.Is(err, io.EOF) { test.Fatal("CLIENT wrong error:", err) } test.Log("CLIENT done") + conn.Close() } func TestEncodeMessageA(test *testing.T) { @@ -87,7 +91,7 @@ func TestEncodeMessageA(test *testing.T) { correct := []byte { 0x58, 0x00, 0xFE, 0xAB, 0xC3, 0x10, 0x4F, 0x04, 0x6B, 0x12, - 0x00, 0x06, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, } if err != nil { @@ -111,9 +115,9 @@ func TestDecodeMessageA(test *testing.T) { transID, method, _, payload, err := decodeMessageA(bytes.NewReader([]byte { 0x58, 0x00, 0xFE, 0xAB, 0xC3, 0x10, 0x4F, 0x04, 0x6B, 0x12, - 0x00, 0x06, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, - })) + }), defaultSizeLimit) if err != nil { test.Fatal(err) } @@ -133,9 +137,9 @@ func TestDecodeMessageAErr(test *testing.T) { _, _, _, _, err := decodeMessageA(bytes.NewReader([]byte { 0x58, 0x00, 0xFE, 0xAB, 0xC3, 0x10, 0x4F, 0x04, 0x6B, 0x12, - 0x01, 0x06, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x06, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, - })) + }), defaultSizeLimit) if !errors.Is(err, io.ErrUnexpectedEOF) { test.Fatalf("wrong error: %v", err) }