diff --git a/assemblyai.go b/assemblyai.go index 449d9a6..67b7b66 100644 --- a/assemblyai.go +++ b/assemblyai.go @@ -12,7 +12,7 @@ import ( ) const ( - version = "1.8.0" + version = "1.8.1" defaultBaseURLScheme = "https" defaultBaseURLHost = "api.assemblyai.com" ) diff --git a/realtime.go b/realtime.go index 3365476..3e3f541 100644 --- a/realtime.go +++ b/realtime.go @@ -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 @@ -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 { diff --git a/realtime_test.go b/realtime_test.go index e1e4739..ed3d30b 100644 --- a/realtime_test.go +++ b/realtime_test.go @@ -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()