1
0
Fork 0
mirror of https://github.com/eosswedenorg/thalos synced 2026-06-16 04:24:56 +02:00

api/client_test.go: test segfaults sometimes because of nil pointers. so setup mock structs and functions.

This commit is contained in:
Henrik Hautakoski 2023-06-05 17:53:03 +02:00
parent c82136aff0
commit cfc6cb9e42

View file

@ -2,8 +2,30 @@ package api
import (
"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) {
tests := []struct {
name string
@ -17,7 +39,9 @@ func TestClient_Subscribe(t *testing.T) {
}
for _, tt := range tests {
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 {
t.Errorf("Client.Subscribe() error = %v, wantErr %v", err, tt.wantErr)
}