summaryrefslogtreecommitdiff
path: root/internal/apicircuit/apicircuit_test.go
blob: 91ee78381285be7c7a0121934c44f292e28ed9df (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
package apicircuit

import (
	"context"
	"errors"
	"testing"
)

func TestExecute_Success(t *testing.T) {
	t.Parallel()

	v, err := Execute(NewRegistry(), "gemini", CapabilityText, func() (string, error) {
		return "ok", nil
	})
	if err != nil || v != "ok" {
		t.Fatalf("Execute() = %q, %v; want ok, nil", v, err)
	}
}

func TestExecute_ContextCanceled(t *testing.T) {
	t.Parallel()

	_, err := Execute(NewRegistry(), "gemini", CapabilityText, func() (string, error) {
		return "", context.Canceled
	})
	if err == nil {
		t.Fatal("expected cancellation to be returned")
	}
	if !errors.Is(err, context.Canceled) {
		t.Fatalf("error = %v, want context.Canceled", err)
	}
}

func TestIsSuccessful_ContextCanceled(t *testing.T) {
	t.Parallel()

	if !isSuccessful(context.Canceled) {
		t.Fatal("context.Canceled should not count as breaker failure")
	}
	if isSuccessful(errors.New("api error")) {
		t.Fatal("arbitrary errors must count as failure")
	}
}