-
Notifications
You must be signed in to change notification settings - Fork 9.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
*: implement a retry logic for auth old revision in the client
- Loading branch information
Showing
4 changed files
with
131 additions
and
1 deletion.
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
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 |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package clientv3 | ||
|
||
import ( | ||
"go.etcd.io/etcd/api/v3/v3rpc/rpctypes" | ||
"go.etcd.io/etcd/client/v3/credentials" | ||
grpccredentials "google.golang.org/grpc/credentials" | ||
"testing" | ||
) | ||
|
||
type dummyAuthTokenBundle struct{} | ||
|
||
func (d dummyAuthTokenBundle) TransportCredentials() grpccredentials.TransportCredentials { | ||
return nil | ||
} | ||
|
||
func (d dummyAuthTokenBundle) PerRPCCredentials() grpccredentials.PerRPCCredentials { | ||
return nil | ||
} | ||
|
||
func (d dummyAuthTokenBundle) NewWithMode(mode string) (grpccredentials.Bundle, error) { | ||
return nil, nil | ||
} | ||
|
||
func (d dummyAuthTokenBundle) UpdateAuthToken(token string) { | ||
} | ||
|
||
func TestClientShouldRefreshToken(t *testing.T) { | ||
type fields struct { | ||
authTokenBundle credentials.Bundle | ||
} | ||
type args struct { | ||
err error | ||
callOpts *options | ||
} | ||
|
||
optsWithTrue := &options { | ||
retryAuth: true, | ||
} | ||
optsWithFalse := &options { | ||
retryAuth: false, | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
fields fields | ||
args args | ||
want bool | ||
}{ | ||
{ | ||
name: "ErrUserEmpty and non nil authTokenBundle", | ||
fields: fields { | ||
authTokenBundle: &dummyAuthTokenBundle{}, | ||
}, | ||
args: args {rpctypes.ErrGRPCUserEmpty, optsWithTrue}, | ||
want: true, | ||
}, | ||
{ | ||
name: "ErrUserEmpty and nil authTokenBundle", | ||
fields: fields { | ||
authTokenBundle: nil, | ||
}, | ||
args: args {rpctypes.ErrGRPCUserEmpty, optsWithTrue}, | ||
want: false, | ||
}, | ||
{ | ||
name: "ErrGRPCInvalidAuthToken and retryAuth", | ||
fields: fields { | ||
authTokenBundle: nil, | ||
}, | ||
args: args {rpctypes.ErrGRPCInvalidAuthToken, optsWithTrue}, | ||
want: true, | ||
}, | ||
{ | ||
name: "ErrGRPCInvalidAuthToken and !retryAuth", | ||
fields: fields { | ||
authTokenBundle: nil, | ||
}, | ||
args: args {rpctypes.ErrGRPCInvalidAuthToken, optsWithFalse}, | ||
want: false, | ||
}, | ||
{ | ||
name: "ErrGRPCAuthOldRevision and retryAuth", | ||
fields: fields { | ||
authTokenBundle: nil, | ||
}, | ||
args: args {rpctypes.ErrGRPCAuthOldRevision, optsWithTrue}, | ||
want: true, | ||
}, | ||
{ | ||
name: "ErrGRPCAuthOldRevision and !retryAuth", | ||
fields: fields { | ||
authTokenBundle: nil, | ||
}, | ||
args: args {rpctypes.ErrGRPCAuthOldRevision, optsWithFalse}, | ||
want: false, | ||
}, | ||
{ | ||
name: "Other error and retryAuth", | ||
fields: fields { | ||
authTokenBundle: nil, | ||
}, | ||
args: args {rpctypes.ErrGRPCAuthFailed, optsWithTrue}, | ||
want: false, | ||
}, | ||
{ | ||
name: "Other error and !retryAuth", | ||
fields: fields { | ||
authTokenBundle: nil, | ||
}, | ||
args: args {rpctypes.ErrGRPCAuthFailed, optsWithFalse}, | ||
want: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
c := &Client{ | ||
authTokenBundle: tt.fields.authTokenBundle, | ||
} | ||
if got := c.shouldRefreshToken(tt.args.err, tt.args.callOpts); got != tt.want { | ||
t.Errorf("shouldRefreshToken() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
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