* move error_accumulator into internal pkg (#304) * move error_accumulator into internal pkg (#304) * add a test for ErrTooManyEmptyStreamMessages in stream_reader (#304)
54 lines
1.4 KiB
Go
54 lines
1.4 KiB
Go
package openai //nolint:testpackage // testing private field
|
|
|
|
import (
|
|
"bufio"
|
|
"bytes"
|
|
"errors"
|
|
"testing"
|
|
|
|
utils "github.com/sashabaranov/go-openai/internal"
|
|
)
|
|
|
|
var errTestUnmarshalerFailed = errors.New("test unmarshaler failed")
|
|
|
|
type failingUnMarshaller struct{}
|
|
|
|
func (*failingUnMarshaller) Unmarshal(_ []byte, _ any) error {
|
|
return errTestUnmarshalerFailed
|
|
}
|
|
|
|
func TestStreamReaderReturnsUnmarshalerErrors(t *testing.T) {
|
|
stream := &streamReader[ChatCompletionStreamResponse]{
|
|
errAccumulator: utils.NewErrorAccumulator(),
|
|
unmarshaler: &failingUnMarshaller{},
|
|
}
|
|
|
|
respErr := stream.unmarshalError()
|
|
if respErr != nil {
|
|
t.Fatalf("Did not return nil with empty buffer: %v", respErr)
|
|
}
|
|
|
|
err := stream.errAccumulator.Write([]byte("{"))
|
|
if err != nil {
|
|
t.Fatalf("%+v", err)
|
|
}
|
|
|
|
respErr = stream.unmarshalError()
|
|
if respErr != nil {
|
|
t.Fatalf("Did not return nil when unmarshaler failed: %v", respErr)
|
|
}
|
|
}
|
|
|
|
func TestStreamReaderReturnsErrTooManyEmptyStreamMessages(t *testing.T) {
|
|
stream := &streamReader[ChatCompletionStreamResponse]{
|
|
emptyMessagesLimit: 3,
|
|
reader: bufio.NewReader(bytes.NewReader([]byte("\n\n\n\n"))),
|
|
errAccumulator: utils.NewErrorAccumulator(),
|
|
unmarshaler: &utils.JSONUnmarshaler{},
|
|
}
|
|
_, err := stream.Recv()
|
|
if !errors.Is(err, ErrTooManyEmptyStreamMessages) {
|
|
t.Fatalf("Did not return error when recv failed: %v", err)
|
|
}
|
|
}
|