File: endpoint_test.go

package info (click to toggle)
websocketd 0.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 612 kB
  • sloc: makefile: 131; sh: 85; ansic: 78; javascript: 65; cs: 58; perl: 51; python: 43; ruby: 41; php: 40; java: 26; haskell: 20
file content (73 lines) | stat: -rw-r--r-- 1,749 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package libwebsocketd

import (
	"strconv"
	"testing"
	"time"
)

var eol_tests = []string{
	"", "\n", "\r\n", "ok\n", "ok\n",
	"quite long string for our test\n",
	"quite long string for our test\r\n",
}

var eol_answers = []string{
	"", "", "", "ok", "ok",
	"quite long string for our test", "quite long string for our test",
}

func TestTrimEOL(t *testing.T) {
	for n := 0; n < len(eol_tests); n++ {
		answ := trimEOL([]byte(eol_tests[n]))
		if string(answ) != eol_answers[n] {
			t.Errorf("Answer '%s' did not match predicted '%s'", answ, eol_answers[n])
		}
	}
}

func BenchmarkTrimEOL(b *testing.B) {
	for n := 0; n < b.N; n++ {
		trimEOL([]byte(eol_tests[n%len(eol_tests)]))
	}
}

type TestEndpoint struct {
	limit  int
	prefix string
	c      chan []byte
	result []string
}

func (e *TestEndpoint) StartReading() {
	go func() {
		for i := 0; i < e.limit; i++ {
			e.c <- []byte(e.prefix + strconv.Itoa(i))
		}
		time.Sleep(time.Millisecond) // should be enough for smaller channel to catch up with long one
		close(e.c)
	}()
}

func (e *TestEndpoint) Terminate() {
}

func (e *TestEndpoint) Output() chan []byte {
	return e.c
}

func (e *TestEndpoint) Send(msg []byte) bool {
	e.result = append(e.result, string(msg))
	return true
}

func TestEndpointPipe(t *testing.T) {
	one := &TestEndpoint{2, "one:", make(chan []byte), make([]string, 0)}
	two := &TestEndpoint{4, "two:", make(chan []byte), make([]string, 0)}
	PipeEndpoints(one, two)
	if len(one.result) != 4 || len(two.result) != 2 {
		t.Errorf("Invalid lengths, should be 4 and 2: %v %v", one.result, two.result)
	} else if one.result[0] != "two:0" || two.result[0] != "one:0" {
		t.Errorf("Invalid first results, should be two:0 and one:0: %#v %#v", one.result[0], two.result[0])
	}
}