// TestSimpleInactivityTimeout tests a simple scene usage // with inactivity timeout. func TestSimpleInactivityTimeout(t *testing.T) { assert := audit.NewTestingAssertion(t, false) scn := scene.StartLimited(100*time.Millisecond, 0) err := scn.Store("foo", 4711) assert.Nil(err) for i := 0; i < 5; i++ { foo, err := scn.Fetch("foo") assert.Nil(err) assert.Equal(foo, 4711) time.Sleep(50) } time.Sleep(100 * time.Millisecond) foo, err := scn.Fetch("foo") assert.True(scene.IsTimeoutError(err)) assert.Nil(foo) status, err := scn.Status() assert.True(scene.IsTimeoutError(err)) assert.Equal(status, scene.Over) err = scn.Stop() assert.True(scene.IsTimeoutError(err)) }
// TestSimpleAbsoluteTimeout tests a simple scene usage // with absolute timeout. func TestSimpleAbsoluteTimeout(t *testing.T) { assert := audit.NewTestingAssertion(t, false) scn := scene.StartLimited(0, 250*time.Millisecond) err := scn.Store("foo", 4711) assert.Nil(err) for { _, err = scn.Fetch("foo") if err != nil { assert.True(scene.IsTimeoutError(err)) break } time.Sleep(50) } err = scn.Stop() assert.True(scene.IsTimeoutError(err)) }
// TestCleanupAfterTimeout tests the cleanup of props after // a timeout. func TestCleanupAfterTimeout(t *testing.T) { assert := audit.NewTestingAssertion(t, false) cleanups := make(map[string]interface{}) cleanup := func(key string, prop interface{}) error { cleanups[key] = prop return nil } scn := scene.StartLimited(0, 100*time.Millisecond) err := scn.StoreClean("foo", 4711, cleanup) assert.Nil(err) err = scn.StoreClean("bar", "yadda", cleanup) assert.Nil(err) time.Sleep(250 * time.Millisecond) err = scn.Stop() assert.True(scene.IsTimeoutError(err)) assert.Length(cleanups, 2) assert.Equal(cleanups["foo"], 4711) assert.Equal(cleanups["bar"], "yadda") }
// requestScene trieves a scene or creates a new one. func (m *cookieSceneManager) requestScene(request *sceneRequest) { // Check availability. scn, ok := m.scenes[request.id] if ok { status, _ := scn.Status() if status == scene.Active { // Active scene found. response := &sceneResponse{ id: scn.ID().String(), scene: scn, } request.responseChan <- response return } } // New scene (first request or expired). scn = scene.StartLimited(m.timeout, 0) response := &sceneResponse{ id: scn.ID().String(), scene: scn, } m.scenes[response.id] = response.scene request.responseChan <- response }