telebit/mplexer/packer/encoder_test.go

122 lines
1.8 KiB
Go
Raw Normal View History

2020-05-19 09:35:22 +00:00
package packer
import (
"context"
"fmt"
"net"
"testing"
"time"
)
func TestEncodeWholeBlock(t *testing.T) {
ch := make(chan string)
2020-05-20 22:23:58 +00:00
a1 := "A.1: hello"
a2 := "A.2: smello"
b1 := "B.1: hello again"
b2 := "B.2: hello a third time"
m := map[string]bool{
a1: false,
a2: false,
b1: false,
b2: false,
}
2020-05-19 09:35:22 +00:00
go func() {
for {
str := <-ch
2020-05-20 22:23:58 +00:00
// TODO check the headers too
if len(str) > 0 && 0xFE == str[0] {
fmt.Printf("TODO header: %q\n", str)
continue
}
b, ok := m[str]
if !ok {
// possible corruption
t.Fatalf("unexpected string %q", str)
}
if b {
// possible corruption also
t.Fatalf("duplicate string %q", str)
}
m[str] = true
2020-05-19 09:35:22 +00:00
}
}()
ctx := context.Background()
2020-05-20 22:23:58 +00:00
rin, wout := net.Pipe()
2020-05-19 09:35:22 +00:00
go func() {
for {
b := make([]byte, 1024)
2020-05-20 22:23:58 +00:00
n, err := rin.Read(b)
2020-05-19 09:35:22 +00:00
if nil != err {
fmt.Printf("Error: %s\n", err)
return
}
r := b[:n]
ch <- string(r)
}
}()
2020-05-20 22:23:58 +00:00
encoder := NewEncoder(ctx, wout)
2020-05-19 09:35:22 +00:00
encoder.Start()
time.Sleep(time.Millisecond)
// single client
go func() {
2020-05-20 22:23:58 +00:00
wout, rin := net.Pipe()
2020-05-19 09:35:22 +00:00
go func() {
2020-05-20 22:23:58 +00:00
wout.Write([]byte(a1))
wout.Write([]byte(a2))
2020-05-19 09:35:22 +00:00
}()
2020-05-20 22:23:58 +00:00
err := encoder.Encode(rin, Addr{
2020-05-19 09:35:22 +00:00
family: "IPv4",
addr: "192.168.1.102",
port: 4834,
2020-05-20 22:23:58 +00:00
})
2020-05-19 09:35:22 +00:00
if nil != err {
fmt.Printf("Enc Err: %q\n", err)
}
}()
// single client
go func() {
2020-05-20 22:23:58 +00:00
wout, rin := net.Pipe()
2020-05-19 09:35:22 +00:00
go func() {
2020-05-20 22:23:58 +00:00
wout.Write([]byte(b1))
wout.Write([]byte(b2))
2020-05-19 09:35:22 +00:00
}()
2020-05-20 22:23:58 +00:00
err := encoder.Encode(rin, Addr{
2020-05-19 09:35:22 +00:00
family: "IPv4",
addr: "192.168.1.103",
port: 4834,
2020-05-20 22:23:58 +00:00
})
2020-05-19 09:35:22 +00:00
if nil != err {
fmt.Printf("Enc Err 2: %q\n", err)
}
}()
2020-05-20 22:23:58 +00:00
// TODO must be a better way to do this
time.Sleep(10 * time.Millisecond)
for k, v := range m {
if !v {
t.Fatalf("failed to encode and transmit a value: %q", k)
}
}
}
/*
func TestEncodeEnd(t *testing.T) {
}
func TestEncodeError(t *testing.T) {
2020-05-19 09:35:22 +00:00
}
2020-05-20 22:23:58 +00:00
*/