|
| 1 | +package server |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + "sync" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/livepeer/go-livepeer/core" |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | +) |
| 16 | + |
| 17 | +// mockLivepeerNode creates a mock LivepeerNode with test pipelines |
| 18 | +func mockLivepeerNode(streamIDs []string) *core.LivepeerNode { |
| 19 | + node := &core.LivepeerNode{ |
| 20 | + LiveMu: &sync.RWMutex{}, |
| 21 | + LivePipelines: make(map[string]*core.LivePipeline), |
| 22 | + } |
| 23 | + |
| 24 | + for _, streamID := range streamIDs { |
| 25 | + pipeline := &core.LivePipeline{ |
| 26 | + StreamID: streamID, |
| 27 | + } |
| 28 | + node.LivePipelines[streamID] = pipeline |
| 29 | + } |
| 30 | + |
| 31 | + return node |
| 32 | +} |
| 33 | + |
| 34 | +func TestSendHeartbeat_Success(t *testing.T) { |
| 35 | + // Test successful heartbeat with valid stream IDs |
| 36 | + streamIDs := []string{"stream1", "stream2", "stream3"} |
| 37 | + node := mockLivepeerNode(streamIDs) |
| 38 | + |
| 39 | + // Create test server to capture the request |
| 40 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 41 | + // Verify request method and headers |
| 42 | + assert.Equal(t, "POST", r.Method) |
| 43 | + assert.Equal(t, "application/json", r.Header.Get("Content-Type")) |
| 44 | + assert.Equal(t, "test-value", r.Header.Get("X-Test-Header")) |
| 45 | + |
| 46 | + // Verify request body |
| 47 | + body, err := io.ReadAll(r.Body) |
| 48 | + require.NoError(t, err) |
| 49 | + |
| 50 | + var requestData map[string][]string |
| 51 | + err = json.Unmarshal(body, &requestData) |
| 52 | + require.NoError(t, err) |
| 53 | + |
| 54 | + ids, ok := requestData["ids"] |
| 55 | + require.True(t, ok) |
| 56 | + require.Len(t, ids, 3) |
| 57 | + |
| 58 | + assert.ElementsMatch(t, ids, streamIDs) |
| 59 | + |
| 60 | + w.WriteHeader(http.StatusOK) |
| 61 | + })) |
| 62 | + defer server.Close() |
| 63 | + |
| 64 | + headers := map[string]string{ |
| 65 | + "X-Test-Header": "test-value", |
| 66 | + } |
| 67 | + |
| 68 | + ctx := context.Background() |
| 69 | + sendHeartbeat(ctx, node, server.URL, headers) |
| 70 | +} |
| 71 | + |
| 72 | +func TestSendHeartbeat_EmptyStreamIDs(t *testing.T) { |
| 73 | + // Test heartbeat with no stream IDs |
| 74 | + node := mockLivepeerNode([]string{}) |
| 75 | + |
| 76 | + // Create test server to capture the request |
| 77 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 78 | + // Verify request body contains empty stream list |
| 79 | + body, err := io.ReadAll(r.Body) |
| 80 | + require.NoError(t, err) |
| 81 | + |
| 82 | + var requestData map[string][]string |
| 83 | + err = json.Unmarshal(body, &requestData) |
| 84 | + require.NoError(t, err) |
| 85 | + |
| 86 | + require.Len(t, requestData["ids"], 0, "ids array should be empty") |
| 87 | + |
| 88 | + w.WriteHeader(http.StatusOK) |
| 89 | + })) |
| 90 | + defer server.Close() |
| 91 | + |
| 92 | + ctx := context.Background() |
| 93 | + sendHeartbeat(ctx, node, server.URL, map[string]string{}) |
| 94 | +} |
0 commit comments