Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ linters:
google.golang.org/protobuf/protoadapt instead, or, better yet,
see if you can move the code generation for that message to the
modern protoc-gen-go.
synctest:
deny:
- pkg: testing/synctest
desc: 'use "github.com/gravitational/teleport/lib/utils/testutils/synctest" instead'
test_packages:
files:
- '!$test'
Expand Down
21 changes: 11 additions & 10 deletions lib/inventory/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (
"github.com/gravitational/teleport/lib/inventory/metadata"
usagereporter "github.com/gravitational/teleport/lib/usagereporter/teleport"
"github.com/gravitational/teleport/lib/utils"
"github.com/gravitational/teleport/lib/utils/testutils/synctest"
)

func TestMain(m *testing.M) {
Expand Down Expand Up @@ -176,7 +177,7 @@ func (a *fakeAuth) UpsertInstance(ctx context.Context, instance types.Instance)
// an ssh service.
func TestSSHServerBasics(t *testing.T) {
t.Parallel()
maybeSynctest(t, testSSHServerBasics)
synctest.Test(t, testSSHServerBasics)
}
func testSSHServerBasics(t *testing.T) {
const serverID = "test-server"
Expand Down Expand Up @@ -383,7 +384,7 @@ func testSSHServerBasics(t *testing.T) {
// an app service.
func TestAppServerBasics(t *testing.T) {
t.Parallel()
maybeSynctest(t, testAppServerBasics)
synctest.Test(t, testAppServerBasics)
}
func testAppServerBasics(t *testing.T) {
const serverID = "test-server"
Expand Down Expand Up @@ -608,7 +609,7 @@ func testAppServerBasics(t *testing.T) {
// a database server.
func TestDatabaseServerBasics(t *testing.T) {
t.Parallel()
maybeSynctest(t, testDatabaseServerBasics)
synctest.Test(t, testDatabaseServerBasics)
}
func testDatabaseServerBasics(t *testing.T) {
const serverID = "test-server"
Expand Down Expand Up @@ -833,7 +834,7 @@ func testDatabaseServerBasics(t *testing.T) {
// TestInstanceHeartbeat verifies basic expected behaviors for instance heartbeat.
func TestInstanceHeartbeat_Disabled(t *testing.T) {
t.Parallel()
maybeSynctest(t, testInstanceHeartbeat_Disabled)
synctest.Test(t, testInstanceHeartbeat_Disabled)
}
func testInstanceHeartbeat_Disabled(t *testing.T) {
const serverID = "test-instance"
Expand Down Expand Up @@ -885,7 +886,7 @@ func TestInstanceHeartbeatDisabledEnv(t *testing.T) {
// TestInstanceHeartbeat verifies basic expected behaviors for instance heartbeat.
func TestInstanceHeartbeat(t *testing.T) {
t.Parallel()
maybeSynctest(t, testInstanceHeartbeat)
synctest.Test(t, testInstanceHeartbeat)
}
func testInstanceHeartbeat(t *testing.T) {
const serverID = "test-instance"
Expand Down Expand Up @@ -990,7 +991,7 @@ func testInstanceHeartbeat(t *testing.T) {
// inventory control stream.
func TestUpdateLabels(t *testing.T) {
t.Parallel()
maybeSynctest(t, testUpdateLabels)
synctest.Test(t, testUpdateLabels)
}
func testUpdateLabels(t *testing.T) {
const serverID = "test-instance"
Expand Down Expand Up @@ -1063,7 +1064,7 @@ func testUpdateLabels(t *testing.T) {
// inventory control stream.
func TestAgentMetadata(t *testing.T) {
t.Parallel()
maybeSynctest(t, testAgentMetadata)
synctest.Test(t, testAgentMetadata)
}
func testAgentMetadata(t *testing.T) {
const serverID = "test-instance"
Expand Down Expand Up @@ -1340,14 +1341,14 @@ func TestGoodbye(t *testing.T) {
}
t.Run(test.name, func(t *testing.T) {
t.Parallel()
maybeSynctest(t, inner)
synctest.Test(t, inner)
})
}
}

func TestKubernetesServerBasics(t *testing.T) {
t.Parallel()
maybeSynctest(t, testKubernetesServerBasics)
synctest.Test(t, testKubernetesServerBasics)
}
func testKubernetesServerBasics(t *testing.T) {
const serverID = "test-server"
Expand Down Expand Up @@ -1575,7 +1576,7 @@ func testKubernetesServerBasics(t *testing.T) {

func TestGetSender(t *testing.T) {
t.Parallel()
maybeSynctest(t, testGetSender)
synctest.Test(t, testGetSender)
}
func testGetSender(t *testing.T) {
controller := NewController(
Expand Down
108 changes: 0 additions & 108 deletions lib/player/player_experimental_test.go

This file was deleted.

67 changes: 67 additions & 0 deletions lib/player/player_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/gravitational/teleport/lib/events"
"github.com/gravitational/teleport/lib/player"
"github.com/gravitational/teleport/lib/session"
"github.com/gravitational/teleport/lib/utils/testutils/synctest"
)

func TestBasicStream(t *testing.T) {
Expand Down Expand Up @@ -392,3 +393,69 @@ func (d *databaseStreamer) sendEvent(ctx context.Context, evts chan apievents.Au
d.idx++
}
}

// TestInterruptsDelay tests that the player responds to playback
// controls even when it is waiting to emit an event.
func TestInterruptsDelay(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
p, err := player.New(&player.Config{
SessionID: "test-session",
Streamer: &simpleStreamer{count: 3, delay: 5000},
})
require.NoError(t, err)

synctest.Wait()

require.NoError(t, p.Play())
t.Cleanup(func() { p.Close() })

synctest.Wait() // player is now waiting to emit event 0

// emulate the user seeking forward while the player is waiting..
start := time.Now()
p.SetPos(10_001 * time.Millisecond)

// expect event 0 and event 1 to be emitted right away
evt0 := <-p.C()
evt1 := <-p.C()
require.Equal(t, int64(0), evt0.GetIndex())
require.Equal(t, int64(1), evt1.GetIndex())

// Time will advance automatically in the synctest bubble.
// This assertion checks that the player was unblocked by
// the SetPos call and not because enough time elapsed.
require.Zero(t, time.Since(start))

<-p.C()

// the user seeked to 10.001 seconds, it should take 4.999
// seconds to get the third event that arrives at second 15.
require.Equal(t, 4999*time.Millisecond, time.Since(start))
})
}

func TestSeekForward(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
p, err := player.New(&player.Config{
SessionID: "test-session",
Streamer: &simpleStreamer{count: 1, delay: 6000},
})
require.NoError(t, err)
t.Cleanup(func() { p.Close() })
require.NoError(t, p.Play())

start := time.Now()

time.Sleep(100 * time.Millisecond)
p.SetPos(500 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
p.SetPos(5900 * time.Millisecond)

<-p.C()

// It should take 300ms for the event to be emitted.
// Two 100ms sleeps (200ms), then a seek to 5.900 seconds which
// requires another 100ms to wait for the event at 6s.
require.Equal(t, 300*time.Millisecond, time.Since(start))
})
}
Loading
Loading