telebit/mplexer/encoder_test.go

147 lines
2.3 KiB
Go
Raw Normal View History

2020-05-22 10:41:24 +00:00
package telebit
2020-05-19 09:35:22 +00:00
import (
"context"
"fmt"
2020-05-20 22:52:06 +00:00
"io"
2020-05-19 09:35:22 +00:00
"net"
2020-05-21 10:29:05 +00:00
"strings"
2020-05-19 09:35:22 +00:00
"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)
2020-05-21 10:29:05 +00:00
parts := strings.Split(str, "\n")
if len(parts) <= 1 {
continue
}
str = parts[1]
2020-05-20 22:23:58 +00:00
}
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
}
}()
2020-05-20 22:52:06 +00:00
// TODO nix context here
ctx := context.TODO()
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 {
2020-05-20 22:52:06 +00:00
t.Fatalf("Error: %s\n", err)
2020-05-19 09:35:22 +00:00
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
2020-05-20 22:52:06 +00:00
go func() {
err := encoder.Run()
if nil != err {
if io.EOF != err {
t.Fatalf("Encoder Run Err: %q\n", err)
}
}
wout.Close()
}()
// TODO eliminate Run and don't sleep here
2020-05-19 09:35:22 +00:00
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-21 10:29:05 +00:00
}, Addr{
scheme: "https",
addr: "example.com",
port: 443,
2020-05-20 22:23:58 +00:00
})
2020-05-19 09:35:22 +00:00
if nil != err {
2020-05-20 22:52:06 +00:00
t.Fatalf("Enc Err 1: %q\n", err)
2020-05-19 09:35:22 +00:00
}
}()
// 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-21 10:29:05 +00:00
}, Addr{
scheme: "https",
addr: "example.com",
port: 443,
2020-05-20 22:23:58 +00:00
})
2020-05-19 09:35:22 +00:00
if nil != err {
2020-05-20 22:52:06 +00:00
t.Fatalf("Enc Err 2: %q\n", err)
2020-05-19 09:35:22 +00:00
}
}()
2020-05-20 22:52:06 +00:00
// TODO use wait group
2020-05-20 22:23:58 +00:00
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
*/