Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

patch(sdk/go): add nil check for c.conn on disconnect & return error #30

Merged
merged 4 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion assemblyai.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

const (
version = "1.8.0"
version = "1.8.1"
defaultBaseURLScheme = "https"
defaultBaseURLHost = "api.assemblyai.com"
)
Expand Down
8 changes: 8 additions & 0 deletions realtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ var (
// ErrDisconnected is returned when attempting to write to a disconnected
// client.
ErrDisconnected = errors.New("client is disconnected")

// ErrConnectionNotFound is returned when attempting to disconnect a
// nil connection
ErrConnectionNotFound = errors.New("client connection does not exist")
)

type MessageType string
Expand Down Expand Up @@ -481,6 +485,10 @@ func (c *RealTimeClient) queryFromOptions() string {
// Disconnect sends the terminate_session message and waits for the server to
// send a SessionTerminated message before closing the connection.
func (c *RealTimeClient) Disconnect(ctx context.Context, waitForSessionTermination bool) error {
if c.conn == nil {
return ErrConnectionNotFound
}

terminate := TerminateSession{TerminateSession: true}

if err := wsjson.Write(ctx, c.conn, terminate); err != nil {
Expand Down
26 changes: 26 additions & 0 deletions realtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,32 @@ func TestRealTime_Connect(t *testing.T) {
require.NoError(t, err)
}

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

// setup webhook server that fails to make a connection
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
apiKey := r.Header.Get("Authorization")
require.Equal(t, "api-key", apiKey)
}))
defer ts.Close()

client := NewRealTimeClientWithOptions(
WithRealTimeAPIKey("api-key"),
WithRealTimeBaseURL(ts.URL),
WithRealTimeTranscriber(&RealTimeTranscriber{}),
)

ctx, cancel := context.WithTimeout(context.Background(), testTimeout)
defer cancel()

// try to connect, note error is returned, but ignore it and proceed to Disconnect
_ = client.Connect(ctx)

err := client.Disconnect(ctx, true)
require.Errorf(t, err, "client connection does not exist")
}

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

Expand Down
Loading