Skip to content

Commit

Permalink
Throttle the http client used in the automation tests so it we don't get
Browse files Browse the repository at this point in the history
rate limited by Okta's API.
  • Loading branch information
monde committed Aug 25, 2021
1 parent 0229250 commit 75575ed
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
5 changes: 4 additions & 1 deletion identity-engine/embedded-auth-with-sdk/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package config

import "net/http"

type Config struct {
Testing bool
Testing bool
HttpClient *http.Client
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,11 @@ func NewTestHarness() *TestHarness {
func (th *TestHarness) InitializeTestSuite(ctx *godog.TestSuiteContext) {
rand.Seed(time.Now().UnixNano())
ctx.BeforeSuite(func() {
httpClient := &http.Client{Timeout: time.Second * 30}
httpClient.Transport = &testThrottledTransport{}
cfg := &config.Config{
Testing: true,
Testing: true,
HttpClient: httpClient,
}
_, client, err := okta.NewClient(
context.Background(),
Expand All @@ -134,6 +137,16 @@ func (th *TestHarness) InitializeTestSuite(ctx *godog.TestSuiteContext) {
})
}

type testThrottledTransport struct{}

func (t *testThrottledTransport) RoundTrip(req *http.Request) (*http.Response, error) {
// Rapid concurrent connections that can be exhibited in an automated test
// harness can get rate limited.
// https://developer.okta.com/docs/reference/rl-additional-limits/
time.Sleep(time.Millisecond * 75)
return http.DefaultTransport.RoundTrip(req)
}

func (th *TestHarness) depopulateMary() {
users, _, _ := th.oktaClient.User.ListUsers(context.Background(), &query.Params{
Q: "Mary",
Expand Down
8 changes: 8 additions & 0 deletions identity-engine/embedded-auth-with-sdk/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ func NewServer(c *config.Config) *Server {
log.Fatalf("new client error: %+v", err)
}

// NOTE: The cucumber testing harness Okta uses to ensure the golang samples
// remain operational needs to be throttled so it doesn't get rate limited
// by too many concurrent requests in tests. The idx client allows the
// ability to set a custom http client and we make use of that feature here.
if c.HttpClient != nil {
idx = idx.WithHTTPClient(c.HttpClient)
}

return &Server{
config: c,
idxClient: idx,
Expand Down

0 comments on commit 75575ed

Please sign in to comment.