mirror of
https://github.com/eosswedenorg/thalos
synced 2026-08-23 19:48:12 +02:00
api/client_test.go: test segfaults sometimes because of nil pointers. so setup mock structs and functions.
This commit is contained in:
parent
c82136aff0
commit
cfc6cb9e42
1 changed files with 25 additions and 1 deletions
|
|
@ -2,8 +2,30 @@ package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/eosswedenorg/thalos/api/message"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type mockReader struct{}
|
||||||
|
|
||||||
|
func (m mockReader) Read(channel Channel) ([]byte, error) {
|
||||||
|
return []byte{}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m mockReader) Close() error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func mockDecoder([]byte, any) error {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func mockHbHandler(message.HeartBeat) {
|
||||||
|
}
|
||||||
|
|
||||||
|
func mockActionHandler(message.ActionTrace) {
|
||||||
|
}
|
||||||
|
|
||||||
func TestClient_Subscribe(t *testing.T) {
|
func TestClient_Subscribe(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
|
|
@ -17,7 +39,9 @@ func TestClient_Subscribe(t *testing.T) {
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
c := Client{}
|
c := NewClient(&mockReader{}, mockDecoder)
|
||||||
|
c.OnHeartbeat = mockHbHandler
|
||||||
|
c.OnAction = mockActionHandler
|
||||||
if err := c.Subscribe(tt.channel); (err != nil) != tt.wantErr {
|
if err := c.Subscribe(tt.channel); (err != nil) != tt.wantErr {
|
||||||
t.Errorf("Client.Subscribe() error = %v, wantErr %v", err, tt.wantErr)
|
t.Errorf("Client.Subscribe() error = %v, wantErr %v", err, tt.wantErr)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue