Compare commits

..

No commits in common. "9bf0c596ba79f1790ecbaaee79092212175e2e2d" and "cbaff8b593fb4c6db83f4c1f9cb086e0a66be574" have entirely different histories.

2 changed files with 13 additions and 27 deletions

View File

@ -103,13 +103,12 @@ func (this *a) unlistTransactionSafe(id int64) {
func (this *a) sendMessageSafe(trans int64, method uint16, data []byte) error {
this.sendLock.Lock()
defer this.sendLock.Unlock()
return encodeMessageA(this.underlying, this.sizeLimit, trans, method, data)
return encodeMessageA(this.underlying, trans, method, data)
}
func (this *a) receive() {
defer func() {
this.underlying.Close()
close(this.transChan)
this.transLock.Lock()
defer this.transLock.Unlock()
for _, trans := range this.transMap {
@ -269,16 +268,7 @@ type incomingMessage struct {
payload []byte
}
func encodeMessageA(
writer io.Writer,
sizeLimit int64,
trans int64,
method uint16,
data []byte,
) error {
if int64(len(data)) > sizeLimit {
return ErrPayloadTooLarge
}
func encodeMessageA(writer io.Writer, trans int64, method uint16, data []byte) error {
buffer := make([]byte, 18 + len(data))
tape.EncodeI64(buffer[:8], trans)
tape.EncodeI16(buffer[8:10], method)

View File

@ -31,23 +31,21 @@ func TestConnA(test *testing.T) {
// server
listener, err := net.Listen(network, addr)
if err != nil { test.Fatal(err) }
test.Cleanup(func() { listener.Close() })
defer 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 }
test.Cleanup(func() { trans.Close() })
defer trans.Close()
for method, payload := range payloads {
test.Log("SERVER m:", method, "p:", payload)
test.Log("SERVER", method, payload)
err := trans.Send(uint16(method), []byte(payload))
if err != nil { test.Error("SERVER", err); return }
}
test.Log("SERVER closing connection")
}()
// client
@ -56,18 +54,18 @@ func TestConnA(test *testing.T) {
if err != nil { test.Fatal("CLIENT", err) }
test.Log("CLIENT dialed")
a := AdaptA(conn, ClientSide)
test.Cleanup(func() { a.Close() })
defer a.Close()
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() })
defer 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 m:", gotMethod, "p:", gotPayload)
test.Log("CLIENT", gotMethod, gotPayload)
if int(gotMethod) != method {
test.Errorf("CLIENT method not equal")
}
@ -75,13 +73,11 @@ 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) {
@ -91,7 +87,7 @@ func TestEncodeMessageA(test *testing.T) {
correct := []byte {
0x58, 0x00, 0xFE, 0xAB, 0xC3, 0x10, 0x4F, 0x04,
0x6B, 0x12,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06,
0x00, 0x06,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
}
if err != nil {
@ -115,9 +111,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, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06,
0x00, 0x06,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
}), defaultSizeLimit)
}))
if err != nil {
test.Fatal(err)
}
@ -137,9 +133,9 @@ func TestDecodeMessageAErr(test *testing.T) {
_, _, _, _, err := decodeMessageA(bytes.NewReader([]byte {
0x58, 0x00, 0xFE, 0xAB, 0xC3, 0x10, 0x4F, 0x04,
0x6B, 0x12,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x06,
0x01, 0x06,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
}), defaultSizeLimit)
}))
if !errors.Is(err, io.ErrUnexpectedEOF) {
test.Fatalf("wrong error: %v", err)
}