Skip to content

Commit bdbe56a

Browse files
authored
fix(test): resolve flaky FVT test (#3993)
Fix flaky FVT test by using Eventually and correct history logic to verify rule status in async scenario. Signed-off-by: Jiyong Huang <huangjy@emqx.io>
1 parent d2d4780 commit bdbe56a

File tree

1 file changed

+34
-14
lines changed

1 file changed

+34
-14
lines changed

fvt/rulestate_test.go

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ func (s *RuleStateTestSuite) TestMulShared() {
505505
s.Run("async run", func() {
506506
wg := sync.WaitGroup{}
507507
wg.Add(6)
508-
final := 0
508+
history := make([]int, 0, 6)
509509
var mu syncx.Mutex
510510
go func() {
511511
defer wg.Done()
@@ -514,7 +514,7 @@ func (s *RuleStateTestSuite) TestMulShared() {
514514
s.Require().NoError(err)
515515
s.Require().Equal(http.StatusOK, resp.StatusCode)
516516
mu.Lock()
517-
final = 0
517+
history = append(history, 0)
518518
mu.Unlock()
519519
}()
520520
go func() {
@@ -524,7 +524,7 @@ func (s *RuleStateTestSuite) TestMulShared() {
524524
s.Require().NoError(err)
525525
s.Require().Equal(http.StatusOK, resp.StatusCode)
526526
mu.Lock()
527-
final = 1
527+
history = append(history, 1)
528528
mu.Unlock()
529529
}()
530530
go func() {
@@ -535,7 +535,7 @@ func (s *RuleStateTestSuite) TestMulShared() {
535535
s.T().Log(GetResponseText(resp))
536536
s.Require().Equal(http.StatusOK, resp.StatusCode)
537537
mu.Lock()
538-
final = 2
538+
history = append(history, 2)
539539
mu.Unlock()
540540
}()
541541
go func() {
@@ -546,7 +546,7 @@ func (s *RuleStateTestSuite) TestMulShared() {
546546
s.T().Log(GetResponseText(resp))
547547
s.Require().Equal(http.StatusOK, resp.StatusCode)
548548
mu.Lock()
549-
final = 3
549+
history = append(history, 3)
550550
mu.Unlock()
551551
}()
552552
go func() {
@@ -556,7 +556,7 @@ func (s *RuleStateTestSuite) TestMulShared() {
556556
s.Require().NoError(err)
557557
s.Require().Equal(http.StatusOK, resp.StatusCode)
558558
mu.Lock()
559-
final = 4
559+
history = append(history, 4)
560560
mu.Unlock()
561561
}()
562562
go func() {
@@ -566,7 +566,7 @@ func (s *RuleStateTestSuite) TestMulShared() {
566566
s.Require().NoError(err)
567567
s.Require().Equal(http.StatusOK, resp.StatusCode)
568568
mu.Lock()
569-
final = 5
569+
history = append(history, 5)
570570
mu.Unlock()
571571
}()
572572
wg.Wait()
@@ -581,14 +581,34 @@ func (s *RuleStateTestSuite) TestMulShared() {
581581
s.True(ok)
582582
s.True(sinkOut1.(float64) > 0)
583583
// mul1 status depends on the final command
584-
metrics, err = client.GetRuleStatus("mul1")
585-
s.Require().NoError(err)
586-
fmt.Println("final", final)
587-
if final == 1 || final == 4 {
588-
s.Equal("stopped", metrics["status"])
589-
} else {
590-
s.Equal("running", metrics["status"])
584+
585+
expectedStatus := "running" // Initial state is running from previous tests
586+
possibleStates := map[string]bool{expectedStatus: true}
587+
588+
s.T().Logf("History: %v", history)
589+
for _, op := range history {
590+
switch op {
591+
case 0, 5: // Start
592+
possibleStates = map[string]bool{"running": true}
593+
case 1, 4: // Stop
594+
possibleStates = map[string]bool{"stopped": true}
595+
case 2: // Update mul1
596+
// If Update sees "Running", it restarts -> Running.
597+
// If Update sees "Stopped", it preserves -> Stopped.
598+
// Since we don't know if it overlapped with a previous properties, it adds "running" to possibilities.
599+
possibleStates["running"] = true
600+
case 3: // Update mul2
601+
// No effect on mul1 status ideally
602+
}
591603
}
604+
605+
s.Eventually(func() bool {
606+
metrics, err = client.GetRuleStatus("mul1")
607+
if err != nil {
608+
return false
609+
}
610+
return possibleStates[metrics["status"].(string)]
611+
}, 1*time.Second, 100*time.Millisecond, "expected one of %v, got %s. history: %v", possibleStates, metrics["status"], history)
592612
})
593613
// Clean
594614
s.Run("clean up", func() {

0 commit comments

Comments
 (0)