Repository navigation
Expand file tree
/
Copy pathsession_timeout_test.go
More file actions
110 lines (85 loc) · 3.16 KB
/
Copy pathsession_timeout_test.go
File metadata and controls
110 lines (85 loc) · 3.16 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package main
import (
"testing"
"time"
"github.com/nareix/joy4/av/pubsub"
"github.com/zorchenhimer/MovieNight/common"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestHLSChannel_ViewerTimeout(t *testing.T) {
// Initialize logging for tests
common.SetupLogging(common.LLDebug, "")
queue := pubsub.NewQueue()
hlsChan, err := NewHLSChannel(queue)
require.NoError(t, err)
defer hlsChan.Close()
// Add a viewer
isNew := hlsChan.AddViewer("test-session")
assert.True(t, isNew, "Should be a new viewer")
assert.Equal(t, 1, hlsChan.GetViewerCount())
// Adding the same viewer again should not be new
isNew = hlsChan.AddViewer("test-session")
assert.False(t, isNew, "Should not be a new viewer")
assert.Equal(t, 1, hlsChan.GetViewerCount())
// Manually trigger cleanup (to avoid waiting 30 seconds in test)
hlsChan.cleanupInactiveViewers()
// Should still have viewer since it's been active recently
assert.Equal(t, 1, hlsChan.GetViewerCount())
// Modify the viewer's last activity to simulate timeout
hlsChan.viewersMutex.Lock()
if viewer, exists := hlsChan.viewers["test-session"]; exists {
viewer.LastActivity = time.Now().Add(-35 * time.Second) // Simulate 35 seconds ago
}
hlsChan.viewersMutex.Unlock()
// Trigger cleanup again
hlsChan.cleanupInactiveViewers()
// Should now have no viewers due to timeout
assert.Equal(t, 0, hlsChan.GetViewerCount())
}
func TestHLSChannel_ViewerActivityUpdate(t *testing.T) {
// Initialize logging for tests
common.SetupLogging(common.LLDebug, "")
queue := pubsub.NewQueue()
hlsChan, err := NewHLSChannel(queue)
require.NoError(t, err)
defer hlsChan.Close()
// Add a viewer
isNew := hlsChan.AddViewer("test-session")
assert.True(t, isNew, "Should be a new viewer")
// Get initial activity time
hlsChan.viewersMutex.RLock()
initialActivity := hlsChan.viewers["test-session"].LastActivity
hlsChan.viewersMutex.RUnlock()
// Wait a small amount and add again (simulating playlist refresh)
time.Sleep(10 * time.Millisecond)
isNew = hlsChan.AddViewer("test-session")
assert.False(t, isNew, "Should not be a new viewer")
// Check that activity was updated
hlsChan.viewersMutex.RLock()
updatedActivity := hlsChan.viewers["test-session"].LastActivity
hlsChan.viewersMutex.RUnlock()
assert.True(t, updatedActivity.After(initialActivity), "Activity should be updated")
}
func TestHLSChannel_BackgroundCleanup(t *testing.T) {
// Initialize logging for tests
common.SetupLogging(common.LLDebug, "")
queue := pubsub.NewQueue()
hlsChan, err := NewHLSChannel(queue)
require.NoError(t, err)
// Add a viewer
hlsChan.AddViewer("test-session")
assert.Equal(t, 1, hlsChan.GetViewerCount())
// Manually set the viewer's activity to past the timeout
hlsChan.viewersMutex.Lock()
if viewer, exists := hlsChan.viewers["test-session"]; exists {
viewer.LastActivity = time.Now().Add(-35 * time.Second)
}
hlsChan.viewersMutex.Unlock()
// The background cleanup should run every 10 seconds, but we'll trigger it manually
hlsChan.cleanupInactiveViewers()
// Viewer should be cleaned up
assert.Equal(t, 0, hlsChan.GetViewerCount())
// Close the channel to test proper cleanup
hlsChan.Close()
}