11package observer
22
33import (
4+ "context"
45 "regexp"
56 "sync"
67 "testing"
@@ -9,7 +10,9 @@ import (
910 "github.com/stretchr/testify/assert"
1011 "github.com/stretchr/testify/require"
1112
13+ "gitlab.com/postgres-ai/database-lab/v3/internal/provision/resources"
1214 "gitlab.com/postgres-ai/database-lab/v3/pkg/client/dblabapi/types"
15+ "gitlab.com/postgres-ai/database-lab/v3/pkg/models"
1316)
1417
1518func TestMaskingField (t * testing.T ) {
@@ -219,3 +222,47 @@ func TestObservingClone_SetOverallError(t *testing.T) {
219222 oc .SetOverallError (false )
220223 assert .False (t , oc .session .state .OverallError )
221224}
225+
226+ func TestObservingClone_RunSessionErrorSignalsDone (t * testing.T ) {
227+ oc := NewObservingClone (types.Config {}, nil )
228+
229+ require .Error (t , oc .RunSession (), "a session that was never initialized cannot run" )
230+
231+ select {
232+ case <- oc .done :
233+ case <- time .After (time .Second ):
234+ t .Fatal ("done must be signalled when RunSession exits with an error" )
235+ }
236+ }
237+
238+ func TestObservingClone_StopExpiredContext (t * testing.T ) {
239+ oc := NewObservingClone (types.Config {}, nil )
240+ oc .session = & Session {SessionID : 42 }
241+
242+ ctx , cancel := context .WithCancel (context .Background ())
243+ cancel ()
244+
245+ err := oc .Stop (ctx )
246+
247+ require .Error (t , err , "Stop must not block when RunSession never started" )
248+ assert .ErrorIs (t , err , context .Canceled )
249+ }
250+
251+ func TestObservingClone_StopUninitializedSession (t * testing.T ) {
252+ oc := NewObservingClone (types.Config {}, nil )
253+
254+ require .Error (t , oc .Stop (context .Background ()))
255+ }
256+
257+ func TestObservingClone_StopTwiceAfterRunSessionExit (t * testing.T ) {
258+ oc := NewObservingClone (types.Config {}, nil )
259+ oc .pool = & resources.Pool {Name : "pool" , MountDir : t .TempDir ()}
260+ oc .session = & Session {SessionID : 7 , Config : types.Config {MaxDuration : 60 }, Result : & models.ObservationResult {}}
261+
262+ close (oc .done )
263+
264+ require .NotPanics (t , func () {
265+ require .NoError (t , oc .Stop (context .Background ()))
266+ require .NoError (t , oc .Stop (context .Background ()))
267+ })
268+ }
0 commit comments