Test using readers and writers with a METADAPT-A connection

This commit is contained in:
Sasha Koshka 2025-11-16 16:01:06 -05:00
parent 5f503021bf
commit 8add67c5de

View File

@ -129,6 +129,65 @@ func TestTransOpenCloseA(test *testing.T) {
clientServerEnvironment(test, clientFunc, serverFunc)
}
func TestReadWriteA(test *testing.T) {
payloads := []string {
"hello",
"world",
"When the impostor is sus!",
}
clientFunc := func(a Conn) {
test.Log("CLIENT accepting transaction")
trans, err := a.AcceptTrans()
if err != nil { test.Fatal("CLIENT", err) }
test.Log("CLIENT accepted transaction")
test.Cleanup(func() { trans.Close() })
for method, payload := range payloads {
test.Log("CLIENT waiting...")
gotMethod, gotReader, err := trans.ReceiveReader()
if err != nil { test.Fatal("CLIENT", err) }
gotPayloadBytes, err := io.ReadAll(gotReader)
if err != nil { test.Fatal("CLIENT", err) }
gotPayload := string(gotPayloadBytes)
test.Log("CLIENT m:", gotMethod, "p:", gotPayload)
if int(gotMethod) != method {
test.Errorf("CLIENT method not equal")
}
if gotPayload != payload {
test.Errorf("CLIENT payload not equal")
}
}
test.Log("CLIENT waiting for transaction close...")
gotMethod, gotPayload, err := trans.Receive()
if !errors.Is(err, io.EOF) {
test.Error("CLIENT wrong error:", err)
test.Error("CLIENT method:", gotMethod)
test.Error("CLIENT payload:", gotPayload)
test.Fatal("CLIENT ok byeeeeeeeeeeeee")
}
test.Log("CLIENT transaction has closed")
}
serverFunc := func(a Conn) {
defer test.Log("SERVER closing connection")
trans, err := a.OpenTrans()
if err != nil { test.Error("SERVER", err); return }
test.Cleanup(func() { trans.Close() })
for method, payload := range payloads {
test.Log("SERVER m:", method, "p:", payload)
func() {
writer, err := trans.SendWriter(uint16(method))
if err != nil { test.Error("SERVER", err); return }
defer writer.Close()
_, err = writer.Write([]byte(payload))
if err != nil { test.Error("SERVER", err); return }
}()
}
}
clientServerEnvironment(test, clientFunc, serverFunc)
}
func TestEncodeMessageA(test *testing.T) {
buffer := new(bytes.Buffer)
payload := []byte { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 }