Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: fix jetstream flaky test #5372

Merged
merged 3 commits into from
Jan 13, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 16 additions & 22 deletions pkg/scalers/nats_jetstream_scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,7 @@ func TestNATSJetStreamIsActive(t *testing.T) {
t.Fatal("Could not parse mock response struct:", err)
}

srv := natsMockHTTPJetStreamServer(t, mockResponseJSON)
defer srv.Close()
client, srv := natsMockHTTPJetStreamServer(t, mockResponseJSON)

ctx := context.Background()
meta, err := parseNATSJetStreamMetadata(&ScalerConfig{TriggerMetadata: mockResponse.metadata.metadataTestData.metadata, TriggerIndex: mockResponse.metadata.triggerIndex})
Expand All @@ -246,7 +245,7 @@ func TestNATSJetStreamIsActive(t *testing.T) {
mockJetStreamScaler := natsJetStreamScaler{
stream: nil,
metadata: meta,
httpClient: http.DefaultClient,
httpClient: client,
logger: InitializeLogger(&ScalerConfig{TriggerMetadata: mockResponse.metadata.metadataTestData.metadata, TriggerIndex: mockResponse.metadata.triggerIndex}, "nats_jetstream_scaler"),
}

Expand Down Expand Up @@ -285,12 +284,7 @@ func TestNATSJetStreamGetMetrics(t *testing.T) {
t.Fatal("Could not parse mock response struct:", err)
}

tr := http.DefaultTransport.(*http.Transport).Clone()
srv := natsMockHTTPJetStreamServer(t, mockResponseJSON)
defer func() {
srv.Close()
http.DefaultTransport = tr
}()
client, srv := natsMockHTTPJetStreamServer(t, mockResponseJSON)

ctx := context.Background()
meta, err := parseNATSJetStreamMetadata(&ScalerConfig{TriggerMetadata: mockResponse.metadata.metadataTestData.metadata, TriggerIndex: mockResponse.metadata.triggerIndex})
Expand All @@ -301,7 +295,7 @@ func TestNATSJetStreamGetMetrics(t *testing.T) {
mockJetStreamScaler := natsJetStreamScaler{
stream: nil,
metadata: meta,
httpClient: http.DefaultClient,
httpClient: client,
logger: InitializeLogger(&ScalerConfig{TriggerMetadata: mockResponse.metadata.metadataTestData.metadata, TriggerIndex: mockResponse.metadata.triggerIndex}, "nats_jetstream_scaler"),
}

Expand All @@ -316,14 +310,17 @@ func TestNATSJetStreamGetMetrics(t *testing.T) {
}
}

func natsMockHTTPJetStreamServer(t *testing.T, mockResponseJSON []byte) *httptest.Server {
func natsMockHTTPJetStreamServer(t *testing.T, mockResponseJSON []byte) (*http.Client, *httptest.Server) {
dialer := &net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}

// redirect leader.localhost for the clustered test
http.DefaultTransport.(*http.Transport).DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
client := &http.Client{
Transport: &http.Transport{},
}
client.Transport.(*http.Transport).DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
if strings.HasSuffix(addr, ".localhost:8222") {
addr = "127.0.0.1:8222"
}
Expand Down Expand Up @@ -367,16 +364,13 @@ func natsMockHTTPJetStreamServer(t *testing.T, mockResponseJSON []byte) *httptes
srv.Listener = l
srv.Start()

return srv
return client, srv
}

func TestNATSJetStreamgetNATSJetstreamMonitoringData(t *testing.T) {
tr := http.DefaultTransport.(*http.Transport).Clone()

invalidJSONServer := natsMockHTTPJetStreamServer(t, []byte(`{invalidJSON}`))
client, invalidJSONServer := natsMockHTTPJetStreamServer(t, []byte(`{invalidJSON}`))
defer func() {
invalidJSONServer.Close()
http.DefaultTransport = tr
}()

ctx := context.Background()
Expand All @@ -388,7 +382,7 @@ func TestNATSJetStreamgetNATSJetstreamMonitoringData(t *testing.T) {
mockJetStreamScaler := natsJetStreamScaler{
stream: nil,
metadata: meta,
httpClient: http.DefaultClient,
httpClient: client,
logger: InitializeLogger(&ScalerConfig{TriggerMetadata: testNATSJetStreamGoodMetadata, TriggerIndex: 0}, "nats_jetstream_scaler"),
}

Expand All @@ -399,7 +393,7 @@ func TestNATSJetStreamgetNATSJetstreamMonitoringData(t *testing.T) {
}

func TestNATSJetStreamGetNATSJetstreamNodeURL(t *testing.T) {
invalidJSONServer := natsMockHTTPJetStreamServer(t, []byte(`{invalidJSON}`))
client, invalidJSONServer := natsMockHTTPJetStreamServer(t, []byte(`{invalidJSON}`))
defer invalidJSONServer.Close()

meta, err := parseNATSJetStreamMetadata(&ScalerConfig{TriggerMetadata: testNATSJetStreamGoodMetadata, TriggerIndex: 0})
Expand All @@ -410,7 +404,7 @@ func TestNATSJetStreamGetNATSJetstreamNodeURL(t *testing.T) {
mockJetStreamScaler := natsJetStreamScaler{
stream: nil,
metadata: meta,
httpClient: http.DefaultClient,
httpClient: client,
logger: InitializeLogger(&ScalerConfig{TriggerMetadata: testNATSJetStreamGoodMetadata, TriggerIndex: 0}, "nats_jetstream_scaler"),
}

Expand All @@ -423,7 +417,7 @@ func TestNATSJetStreamGetNATSJetstreamNodeURL(t *testing.T) {
}

func TestNATSJetStreamGetNATSJetstreamServerURL(t *testing.T) {
invalidJSONServer := natsMockHTTPJetStreamServer(t, []byte(`{invalidJSON}`))
client, invalidJSONServer := natsMockHTTPJetStreamServer(t, []byte(`{invalidJSON}`))
defer invalidJSONServer.Close()

meta, err := parseNATSJetStreamMetadata(&ScalerConfig{TriggerMetadata: testNATSJetStreamGoodMetadata, TriggerIndex: 0})
Expand All @@ -434,7 +428,7 @@ func TestNATSJetStreamGetNATSJetstreamServerURL(t *testing.T) {
mockJetStreamScaler := natsJetStreamScaler{
stream: nil,
metadata: meta,
httpClient: http.DefaultClient,
httpClient: client,
logger: InitializeLogger(&ScalerConfig{TriggerMetadata: testNATSJetStreamGoodMetadata, TriggerIndex: 0}, "nats_jetstream_scaler"),
}

Expand Down
Loading