Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gagantrivedi committed Dec 11, 2024
1 parent 89b3cea commit e35a6ef
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
16 changes: 10 additions & 6 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"sync"
"testing"
"time"

flagsmith "github.com/Flagsmith/flagsmith-go-client/v4"
"github.com/Flagsmith/flagsmith-go-client/v4/fixtures"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -865,14 +864,18 @@ func TestPollErrorHandlerIsUsedWhenPollFails(t *testing.T) {

func TestRealtime(t *testing.T) {
// Given
requestCount := 0
mux := http.NewServeMux()
requestCount := struct {
mu sync.Mutex
count int
}{}

mux.HandleFunc("/api/v1/environment-document/", func(rw http.ResponseWriter, req *http.Request) {
assert.Equal(t, "GET", req.Method)
fmt.Println(req.URL.Path)
assert.Equal(t, fixtures.EnvironmentAPIKey, req.Header.Get("X-Environment-Key"))

requestCount += 1
requestCount.mu.Lock()
requestCount.count++
requestCount.mu.Unlock()

rw.Header().Set("Content-Type", "application/json")
rw.WriteHeader(http.StatusOK)
Expand Down Expand Up @@ -936,7 +939,8 @@ func TestRealtime(t *testing.T) {
// (After the second sse event)
time.Sleep(10 * time.Millisecond)

assert.Equal(t, 2, requestCount)
requestCount.mu.Lock()
assert.Equal(t, 2, requestCount.count)
}
func sendUpdatedAtSSEEvent(rw http.ResponseWriter, flusher http.Flusher, updatedAt float64) {
// Format the SSE event with the provided updatedAt value
Expand Down
5 changes: 4 additions & 1 deletion realtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func (c *Client) startRealtimeUpdates(ctx context.Context) {
resp, err := http.Get(stream_url)
if err != nil {
c.log.Errorf("Error connecting to realtime server: %v", err)
continue
}
defer resp.Body.Close()

Expand All @@ -38,11 +39,13 @@ func (c *Client) startRealtimeUpdates(ctx context.Context) {
parsedTime, err := parseUpdatedAtFromSSE(line)
if err != nil {
c.log.Errorf("Error reading realtime stream: %v", err)
return
}
if parsedTime.After(envUpdatedAt) {
err = c.UpdateEnvironment(ctx)
if err != nil {
c.log.Errorf("Failed to update the environment: %v", err)
continue
}
env, _ := c.environment.Load().(*environments.EnvironmentModel)

Expand All @@ -51,7 +54,7 @@ func (c *Client) startRealtimeUpdates(ctx context.Context) {
}
}
if err := scanner.Err(); err != nil {
c.log.Errorf("Error realtime stream: %v", err)
c.log.Errorf("Error reading realtime stream: %v", err)
}
}
}
Expand Down

0 comments on commit e35a6ef

Please sign in to comment.