Skip to content

Commit 38d76d2

Browse files
committed
fix: remove authenticating and add test for login/logout status change
1 parent cfc87d4 commit 38d76d2

3 files changed

Lines changed: 36 additions & 16 deletions

File tree

foreign/go/client/tcp/tcp_core_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,39 @@ func TestNewIggyTcpClient_StoresProvidedLogger(t *testing.T) {
274274
}
275275
}
276276

277+
func TestLoginUser_LoginAndLogout(t *testing.T) {
278+
c, serverConn := newTestClient(t)
279+
280+
identity := make([]byte, 4)
281+
binary.LittleEndian.PutUint32(identity, 42)
282+
283+
go func() {
284+
serverRespond(t, serverConn, 0, identity)
285+
// login always probes for a leader afterwards; it must be answered or the call blocks.
286+
serverRespond(t, serverConn, uint32(ierror.FeatureUnavailableCode), nil)
287+
serverRespond(t, serverConn, 0, nil)
288+
}()
289+
290+
ctx := context.Background()
291+
info, err := c.LoginUser(ctx, "iggy", "iggy")
292+
if err != nil {
293+
t.Fatalf("unexpected login error: %v", err)
294+
}
295+
if info.UserId != 42 {
296+
t.Errorf("got user id %d, want 42", info.UserId)
297+
}
298+
if c.sessionState != iggcon.SessionStateAuthenticated {
299+
t.Errorf("expected session %v after login, got %v", iggcon.SessionStateAuthenticated, c.sessionState)
300+
}
301+
302+
if err := c.LogoutUser(ctx); err != nil {
303+
t.Fatalf("unexpected logout error: %v", err)
304+
}
305+
if c.sessionState != iggcon.SessionStateUnauthenticated {
306+
t.Errorf("expected session %v after logout, got %v", iggcon.SessionStateUnauthenticated, c.sessionState)
307+
}
308+
}
309+
277310
func TestLoginUser_RejectedReloginKeepsExistingSession(t *testing.T) {
278311
c, serverConn := newTestClient(t)
279312
c.sessionState = iggcon.SessionStateAuthenticated

foreign/go/client/tcp/tcp_session_management.go

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,12 @@ func (c *IggyTcpClient) LoginWithPersonalAccessToken(ctx context.Context, token
4444

4545
func (c *IggyTcpClient) login(ctx context.Context, loginCmd command.Command) (*iggcon.IdentityInfo, error) {
4646
c.logger.Info("Iggy client is signing in...", slog.String("client_address", c.clientAddress))
47-
c.mtx.Lock()
48-
pre := c.sessionState
49-
c.sessionState = iggcon.SessionStateAuthenticating
50-
c.mtx.Unlock()
5147

48+
// A failed login never writes the session state: a server-side reject
49+
// leaves the existing session untouched, and a connection that dies
50+
// mid-attempt is already reset to unauthenticated by invalidateConnLocked.
5251
buffer, err := c.do(ctx, loginCmd)
5352
if err != nil {
54-
// A rejected login leaves the session state untouched, so restore the
55-
// pre-login state only while it is still Authenticating: if the state
56-
// moved, the connection may died mid-attempt and invalidation already
57-
// recorded it unauthenticated, skip the restoration.
58-
c.mtx.Lock()
59-
if c.sessionState == iggcon.SessionStateAuthenticating {
60-
c.sessionState = pre
61-
}
62-
c.mtx.Unlock()
6353
return nil, err
6454
}
6555

foreign/go/contracts/state.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,13 @@ type SessionState uint8
4545

4646
const (
4747
SessionStateUnauthenticated SessionState = iota
48-
SessionStateAuthenticating
4948
SessionStateAuthenticated
5049
)
5150

5251
func (s SessionState) String() string {
5352
switch s {
5453
case SessionStateUnauthenticated:
5554
return "unauthenticated"
56-
case SessionStateAuthenticating:
57-
return "authenticating"
5855
case SessionStateAuthenticated:
5956
return "authenticated"
6057
default:

0 commit comments

Comments
 (0)