Skip to content

Commit d817f7d

Browse files
committed
Update confidential_test.go
1 parent d82d813 commit d817f7d

File tree

1 file changed

+23
-19
lines changed

1 file changed

+23
-19
lines changed

apps/confidential/confidential_test.go

+23-19
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
"net/url"
1818
"os"
1919
"path/filepath"
20-
"reflect"
2120
"strings"
2221
"sync"
2322
"testing"
@@ -826,35 +825,26 @@ func TestRefreshInMultipleRequests(t *testing.T) {
826825
Token string
827826
Tenant string
828827
}
829-
ch := make(chan tokenResult, 10)
828+
tokenOneCchecker := false
829+
tokenTwoCchecker := false
830+
831+
ch := make(chan tokenResult, 14)
830832
var mu sync.Mutex // Mutex to protect access to expectedResponse
831-
expectedResponse := []tokenResult{
832-
{Token: "new token", Tenant: "firstTentant"},
833-
{Token: "new token", Tenant: "firstTentant"},
834-
{Token: "first token", Tenant: "secondTentant"},
835-
{Token: "first token", Tenant: "secondTentant"},
836-
{Token: "first token", Tenant: "secondTentant"},
837-
{Token: "first token", Tenant: "firstTentant"},
838-
{Token: "first token", Tenant: "secondTentant"},
839-
{Token: "new token", Tenant: "firstTentant"},
840-
{Token: "new token", Tenant: "firstTentant"},
841-
{Token: "first token", Tenant: "secondTentant"},
842-
}
843833
gotResponse := []tokenResult{}
844834
mockClient.AppendResponse(
845-
mock.WithBody([]byte(fmt.Sprintf(`{"access_token":%q,"expires_in":%d,"refresh_in":%d,"token_type":"Bearer"}`, secondToken, expiresIn, refreshIn))), mock.WithCallback(func(req *http.Request) {
835+
mock.WithBody([]byte(fmt.Sprintf(`{"access_token":%q,"expires_in":%d,"refresh_in":%d,"token_type":"Bearer"}`, secondToken+"firstTenant", expiresIn, refreshIn))), mock.WithCallback(func(req *http.Request) {
846836
time.Sleep(150 * time.Millisecond)
847837
}),
848838
)
849839
mockClient.AppendResponse(
850-
mock.WithBody([]byte(fmt.Sprintf(`{"access_token":%q,"expires_in":%d,"refresh_in":%d,"token_type":"Bearer"}`, secondToken, expiresIn, refreshIn))), mock.WithCallback(func(req *http.Request) {
840+
mock.WithBody([]byte(fmt.Sprintf(`{"access_token":%q,"expires_in":%d,"refresh_in":%d,"token_type":"Bearer"}`, secondToken+"secondTenant", expiresIn, refreshIn))), mock.WithCallback(func(req *http.Request) {
851841
time.Sleep(100 * time.Millisecond)
852842
mu.Lock()
853843
base.GetCurrentTime = originalTime
854844
mu.Unlock()
855845
}),
856846
)
857-
for i := 0; i < 5; i++ {
847+
for i := 0; i < 7; i++ {
858848
wg.Add(1)
859849
wg.Add(1)
860850
time.Sleep(50 * time.Millisecond)
@@ -865,6 +855,13 @@ func TestRefreshInMultipleRequests(t *testing.T) {
865855
t.Error(err)
866856
return
867857
}
858+
if ar.AccessToken == secondToken+"firstTenant" && ar.Metadata.TokenSource == base.IdentityProvider {
859+
if tokenOneCchecker {
860+
t.Error("Error can only call this once")
861+
} else {
862+
tokenOneCchecker = true
863+
}
864+
}
868865
ch <- tokenResult{Token: ar.AccessToken, Tenant: "firstTentant"} // Send result to channel
869866
}()
870867
go func() {
@@ -875,6 +872,13 @@ func TestRefreshInMultipleRequests(t *testing.T) {
875872
t.Error(err)
876873
return
877874
}
875+
if ar.AccessToken == secondToken+"secondTenant" && ar.Metadata.TokenSource == base.IdentityProvider {
876+
if tokenTwoCchecker {
877+
t.Error("Error can only call this once")
878+
} else {
879+
tokenTwoCchecker = true
880+
}
881+
}
878882
ch <- tokenResult{Token: ar.AccessToken, Tenant: "secondTentant"} // Send result to channel
879883
}()
880884
}
@@ -885,8 +889,8 @@ func TestRefreshInMultipleRequests(t *testing.T) {
885889
gotResponse = append(gotResponse, s)
886890
mu.Unlock() // Release lock after modifying expectedResponse
887891
}
888-
if reflect.DeepEqual(gotResponse, expectedResponse) {
889-
t.Error("gotResponse and expectedResponse are not equal")
892+
if !tokenOneCchecker && !tokenTwoCchecker {
893+
t.Error("Error should be called at least once")
890894
}
891895
}()
892896
wg.Wait()

0 commit comments

Comments
 (0)