summaryrefslogtreecommitdiff
path: root/error_buffer_test.go
blob: 81107935967d9dceb5cfe2e446714a4084865a62 (plain)
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
74
75
76
package roadrunner

import (
	"github.com/stretchr/testify/assert"
	"testing"
)

func TestErrBuffer_Write_Len(t *testing.T) {
	buf := newErrBuffer()
	defer buf.Close()

	buf.Write([]byte("hello"))
	assert.Equal(t, 5, buf.Len())
	assert.Equal(t, "hello", buf.String())
}

func TestErrBuffer_Write_Event(t *testing.T) {
	buf := newErrBuffer()
	defer buf.Close()

	tr := make(chan interface{})
	buf.Listen(func(event int, ctx interface{}) {
		assert.Equal(t, EventStderrOutput, event)
		assert.Equal(t, []byte("hello\n"), ctx)
		close(tr)
	})

	buf.Write([]byte("hello\n"))

	<-tr

	// messages are read
	assert.Equal(t, 0, buf.Len())
}

func TestErrBuffer_Write_Event_Separated(t *testing.T) {
	buf := newErrBuffer()
	defer buf.Close()

	tr := make(chan interface{})
	buf.Listen(func(event int, ctx interface{}) {
		assert.Equal(t, EventStderrOutput, event)
		assert.Equal(t, []byte("hello\nending"), ctx)
		close(tr)
	})

	buf.Write([]byte("hel"))
	buf.Write([]byte("lo\n"))
	buf.Write([]byte("ending"))

	<-tr
	assert.Equal(t, 0, buf.Len())
	assert.Equal(t, "", buf.String())
}

func TestErrBuffer_Write_Event_Separated_NoListener(t *testing.T) {
	buf := newErrBuffer()
	defer buf.Close()

	buf.Write([]byte("hel"))
	buf.Write([]byte("lo\n"))
	buf.Write([]byte("ending"))

	assert.Equal(t, 12, buf.Len())
	assert.Equal(t, "hello\nending", buf.String())
}

func TestErrBuffer_Write_Remaining(t *testing.T) {
	buf := newErrBuffer()
	defer buf.Close()

	buf.Write([]byte("hel"))

	assert.Equal(t, 3, buf.Len())
	assert.Equal(t, "hel", buf.String())
}