forked from ZbigniewTomanek/pgvertica
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message_buffer_test.go
61 lines (48 loc) · 1.34 KB
/
message_buffer_test.go
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
package pgvertica
import (
"fmt"
"testing"
"github.com/jackc/pgproto3/v2"
"github.com/stretchr/testify/assert"
)
type MockMessage struct {
pgproto3.Message
}
func (mm *MockMessage) Encode(dst []byte) []byte {
if dst == nil {
return make([]byte, 'a')
} else {
return append(dst, 'a')
}
}
func TestQueueMessages(t *testing.T) {
mb := newMessagesBuffer(&MockConn{})
mb.queueMessages(&MockMessage{})
assert.Equal(t, 1, mb.buffSize(), "The buffer size should be 1 after adding one message")
}
func TestSendQueuedMessages(t *testing.T) {
{
mb := newMessagesBuffer(&MockConn{})
mb.queueMessages(&MockMessage{})
err := mb.sendQueuedMessages()
assert.NoError(t, err, "Should not return error on happy path")
assert.Equal(t, 0, mb.buffSize(), "The buffer should be empty after sending messages")
}
{
mb := newMessagesBuffer(&MockConn{})
mb.queueMessages(&MockMessage{})
mb.conn.(*MockConn).err = fmt.Errorf("error")
err := mb.sendQueuedMessages()
assert.Error(t, err, "Should return error if Write returns error")
}
}
func TestWriteMessages(t *testing.T) {
{
err := writeMessages(&MockConn{}, &MockMessage{})
assert.NoError(t, err, "Should not return error on happy path")
}
{
err := writeMessages(&MockConn{err: fmt.Errorf("error")}, &MockMessage{})
assert.Error(t, err, "Should return error if Write returns error")
}
}