internal/mock: Add mock transaction implementation
This commit is contained in:
parent
8add67c5de
commit
5e965def7c
64
internal/mock/connection.go
Normal file
64
internal/mock/connection.go
Normal file
@ -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())
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user