-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
54dd4bb
commit 984999b
Showing
2 changed files
with
135 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,7 +22,10 @@ import ( | |
"testing" | ||
"time" | ||
|
||
"github.com/google/uuid" | ||
"github.com/gravitational/trace" | ||
"github.com/stretchr/testify/require" | ||
"golang.org/x/sync/errgroup" | ||
|
||
"github.com/gravitational/teleport/api/client/proto" | ||
"github.com/gravitational/teleport/api/types" | ||
|
@@ -34,6 +37,8 @@ import ( | |
type accessRequestServices struct { | ||
types.Events | ||
services.DynamicAccessExt | ||
|
||
bk *memory.Memory | ||
} | ||
|
||
func newAccessRequestPack(t *testing.T) (accessRequestServices, *services.AccessRequestCache) { | ||
|
@@ -43,11 +48,13 @@ func newAccessRequestPack(t *testing.T) (accessRequestServices, *services.Access | |
svcs := accessRequestServices{ | ||
Events: local.NewEventsService(bk), | ||
DynamicAccessExt: local.NewDynamicAccessService(bk), | ||
bk: bk, | ||
} | ||
|
||
cache, err := services.NewAccessRequestCache(services.AccessRequestCacheConfig{ | ||
Events: svcs, | ||
Getter: svcs, | ||
Events: svcs, | ||
Getter: svcs, | ||
MaxRetryPeriod: time.Millisecond * 100, | ||
}) | ||
require.NoError(t, err) | ||
|
||
|
@@ -60,6 +67,108 @@ func newAccessRequestPack(t *testing.T) (accessRequestServices, *services.Access | |
return svcs, cache | ||
} | ||
|
||
func TestAccessRequestCacheResets(t *testing.T) { | ||
const ( | ||
requestCount = 100 | ||
workers = 20 | ||
resets = 3 | ||
) | ||
|
||
t.Parallel() | ||
|
||
svcs, cache := newAccessRequestPack(t) | ||
defer cache.Close() | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
for i := 0; i < requestCount; i++ { | ||
r, err := types.NewAccessRequest(uuid.New().String(), "[email protected]", "some-role") | ||
require.NoError(t, err) | ||
|
||
_, err = svcs.CreateAccessRequestV2(ctx, r) | ||
require.NoError(t, err) | ||
} | ||
|
||
timeout := time.After(time.Second * 30) | ||
|
||
for { | ||
rsp, err := cache.ListAccessRequests(ctx, &proto.ListAccessRequestsRequest{ | ||
Limit: requestCount, | ||
}) | ||
require.NoError(t, err) | ||
if len(rsp.AccessRequests) == requestCount { | ||
break | ||
} | ||
|
||
select { | ||
case <-timeout: | ||
require.FailNow(t, "timeout waiting for access request cache to populate") | ||
case <-time.After(time.Millisecond * 200): | ||
} | ||
} | ||
|
||
doneC := make(chan struct{}) | ||
reads := make(chan struct{}, workers) | ||
var eg errgroup.Group | ||
|
||
for i := 0; i < workers; i++ { | ||
eg.Go(func() error { | ||
for { | ||
select { | ||
case <-doneC: | ||
return nil | ||
case <-time.After(time.Millisecond * 20): | ||
} | ||
|
||
rsp, err := cache.ListAccessRequests(ctx, &proto.ListAccessRequestsRequest{ | ||
Limit: int32(requestCount), | ||
}) | ||
if err != nil { | ||
return trace.Errorf("unexpected read failure: %v", err) | ||
} | ||
|
||
select { | ||
case reads <- struct{}{}: | ||
default: | ||
} | ||
|
||
if len(rsp.AccessRequests) != requestCount { | ||
return trace.Errorf("unexpected number of access requests: %d (expected %d)", len(rsp.AccessRequests), requestCount) | ||
} | ||
} | ||
}) | ||
} | ||
|
||
inits := make(chan struct{}, resets+1) | ||
cache.SetInitCallback(func() { | ||
inits <- struct{}{} | ||
}) | ||
|
||
timeout = time.After(time.Second * 30) | ||
for i := 0; i < resets; i++ { | ||
svcs.bk.CloseWatchers() | ||
select { | ||
case <-inits: | ||
case <-timeout: | ||
require.FailNowf(t, "timeout waiting for access request cache to reset", "reset=%d", i) | ||
} | ||
|
||
for j := 0; j < workers; j++ { | ||
// ensure that we're not racing ahead of worker reads too | ||
// much if inits are happening quickly. | ||
select { | ||
case <-reads: | ||
case <-timeout: | ||
require.FailNowf(t, "timeout waiting for worker reads to catch up", "reset=%d", i) | ||
} | ||
} | ||
} | ||
|
||
close(doneC) | ||
require.NoError(t, eg.Wait()) | ||
} | ||
|
||
// TestAccessRequestCacheBasics verifies the basic expected behaviors of the access request cache, | ||
// including correct sorting and handling of put/delete events. | ||
func TestAccessRequestCacheBasics(t *testing.T) { | ||
|