talos/internal/integration/api/events.go
Andrey Smirnov 3e100aa977
test: workaround EventsWatch test flakiness
This test sometimes fails with a message like:

```
=== RUN   TestIntegration/api.EventsSuite/TestEventsWatch
    assertion_compare.go:323:
        	Error Trace:	events.go:88
        	Error:      	"0" is not greater than or equal to "14"
        	Test:       	TestIntegration/api.EventsSuite/TestEventsWatch
        	Messages:   	[]
```

I believe the root cause is that the initial (first event) delivery
might be more than 100ms, so instead of waiting for 100ms for each
event, block for 500ms for all events to arrive.

Signed-off-by: Andrey Smirnov <andrey.smirnov@talos-systems.com>
2021-10-15 12:51:56 +03:00

97 lines
2.5 KiB
Go

// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//go:build integration_api
// +build integration_api
package api
import (
"context"
"time"
"github.com/talos-systems/talos/internal/integration/base"
"github.com/talos-systems/talos/pkg/machinery/client"
machinetype "github.com/talos-systems/talos/pkg/machinery/config/types/v1alpha1/machine"
)
// EventsSuite verifies Events API.
type EventsSuite struct {
base.APISuite
ctx context.Context
ctxCancel context.CancelFunc
nodeCtx context.Context
}
// SuiteName ...
func (suite *EventsSuite) SuiteName() string {
return "api.EventsSuite"
}
// SetupTest ...
func (suite *EventsSuite) SetupTest() {
// make sure API calls have timeout
suite.ctx, suite.ctxCancel = context.WithTimeout(context.Background(), 30*time.Second)
suite.nodeCtx = client.WithNodes(suite.ctx, suite.RandomDiscoveredNode(machinetype.TypeWorker))
}
// TearDownTest ...
func (suite *EventsSuite) TearDownTest() {
if suite.ctxCancel != nil {
suite.ctxCancel()
}
}
// TestEventsWatch verifies events watch API.
func (suite *EventsSuite) TestEventsWatch() {
receiveEvents := func(opts ...client.EventsOptionFunc) []client.Event {
result := []client.Event{}
watchCtx, watchCtxCancel := context.WithCancel(suite.nodeCtx)
defer watchCtxCancel()
suite.Assert().NoError(suite.Client.EventsWatch(watchCtx, func(ch <-chan client.Event) {
defer watchCtxCancel()
timer := time.NewTimer(500 * time.Millisecond)
defer timer.Stop()
for {
select {
case event, ok := <-ch:
if !ok {
return
}
result = append(result, event)
case <-timer.C:
return
}
}
}, opts...))
return result
}
allEvents := receiveEvents(client.WithTailEvents(-1))
suite.Require().Greater(len(allEvents), 20)
suite.Assert().Len(receiveEvents(), 0)
suite.Assert().Len(receiveEvents(client.WithTailEvents(5)), 5)
suite.Assert().Len(receiveEvents(client.WithTailEvents(20)), 20)
// pick some ID of 15th event in the past; API should return at least 14 events
// (as check excludes that event with picked ID)
id := allEvents[len(allEvents)-15].ID
eventsSinceID := receiveEvents(client.WithTailID(id))
suite.Require().GreaterOrEqual(len(eventsSinceID), 14) // there might some new events since allEvents, but at least 15 should be received
}
func init() {
allSuites = append(allSuites, new(EventsSuite))
}