From 5e965def7c68278946de2d8229f524519e7ed5de Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Mon, 17 Nov 2025 16:49:09 -0500 Subject: [PATCH] internal/mock: Add mock transaction implementation --- internal/mock/connection.go | 64 +++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 internal/mock/connection.go diff --git a/internal/mock/connection.go b/internal/mock/connection.go new file mode 100644 index 0000000..31a7b55 --- /dev/null +++ b/internal/mock/connection.go @@ -0,0 +1,64 @@ +package mock + +import "io" +import "time" +import "bytes" +import "git.tebibyte.media/sashakoshka/hopp" + +var _ hopp.Trans = new(Trans) + +// Trans is a mock transaction implementation. +type Trans struct { + // These arrays must be the same length. You can load this up + // with messages to read, or deposit messages and retrieve them + // here later. + Methods []uint16 + Messages [][]byte +} + +func (this *Trans) Close() error { return nil } + +func (this *Trans) ID() int64 { return 56 } + +func (this *Trans) Send(method uint16, data []byte) error { + this.Methods = append(this.Methods, method) + this.Messages = append(this.Messages, data) + return nil +} + +func (this *Trans) SendWriter(method uint16) (io.WriteCloser, error) { + return &transWriter { + Buffer: new(bytes.Buffer), + method: method, + parent: this, + }, nil +} + +func (this *Trans) Receive() (method uint16, data []byte, err error) { + if len(this.Methods) == 0 { + return 0, nil, io.EOF + } + method = this.Methods[0] + data = this.Messages[0] + this.Methods = this.Methods[1:] + this.Messages = this.Messages[1:] + return method, data, nil +} + +func (this *Trans) ReceiveReader() (method uint16, reader io.Reader, err error) { + method, data, err := this.Receive() + if err != nil { return 0, nil, err } + return method, bytes.NewBuffer(data), nil +} + +func (this *Trans) SetDeadline(time.Time) error { return nil } + +type transWriter struct { + *bytes.Buffer + method uint16 + parent *Trans +} + +func (this *transWriter) Close() error { + return this.parent.Send(this.method, this.Bytes()) +}