소스 검색

Remove sleeps in test code by overriding time.Now()

Sean Lafferty 6 년 전
부모
커밋
b3461c9806
2개의 변경된 파일10개의 추가작업 그리고 6개의 파일을 삭제
  1. 4 2
      pkg/services/sqlstore/user_auth.go
  2. 6 4
      pkg/services/sqlstore/user_auth_test.go

+ 4 - 2
pkg/services/sqlstore/user_auth.go

@@ -10,6 +10,8 @@ import (
 	"github.com/grafana/grafana/pkg/util"
 )
 
+var getTime = time.Now
+
 func init() {
 	bus.AddHandler("sql", GetUserByAuthInfo)
 	bus.AddHandler("sql", GetAuthInfo)
@@ -153,7 +155,7 @@ func SetAuthInfo(cmd *m.SetAuthInfoCommand) error {
 			UserId:     cmd.UserId,
 			AuthModule: cmd.AuthModule,
 			AuthId:     cmd.AuthId,
-			Created:    time.Now(),
+			Created:    getTime(),
 		}
 
 		if cmd.OAuthToken != nil {
@@ -187,7 +189,7 @@ func UpdateAuthInfo(cmd *m.UpdateAuthInfoCommand) error {
 			UserId:     cmd.UserId,
 			AuthModule: cmd.AuthModule,
 			AuthId:     cmd.AuthId,
-			Created:    time.Now(),
+			Created:    getTime(),
 		}
 
 		if cmd.OAuthToken != nil {

+ 6 - 4
pkg/services/sqlstore/user_auth_test.go

@@ -175,17 +175,21 @@ func TestUserAuth(t *testing.T) {
 			login := "loginuser0"
 
 			// Calling GetUserByAuthInfoQuery on an existing user will populate an entry in the user_auth table
+			// Make the first log-in during the past
+			getTime = func() time.Time { return time.Now().AddDate(0, 0, -2) }
 			query := &m.GetUserByAuthInfoQuery{Login: login, AuthModule: "test1", AuthId: "test1"}
 			err = GetUserByAuthInfo(query)
+			getTime = time.Now
 
 			So(err, ShouldBeNil)
 			So(query.Result.Login, ShouldEqual, login)
 
 			// Add a second auth module for this user
-			// resolution of `Created` column is 1sec, so we need a delay
-			time.Sleep(time.Second)
+			// Have this module's last log-in be more recent
+			getTime = func() time.Time { return time.Now().AddDate(0, 0, -1) }
 			query = &m.GetUserByAuthInfoQuery{Login: login, AuthModule: "test2", AuthId: "test2"}
 			err = GetUserByAuthInfo(query)
+			getTime = time.Now
 
 			So(err, ShouldBeNil)
 			So(query.Result.Login, ShouldEqual, login)
@@ -201,8 +205,6 @@ func TestUserAuth(t *testing.T) {
 			So(getAuthQuery.Result.AuthModule, ShouldEqual, "test2")
 
 			// "log in" again with the first auth module
-			// resolution of `Created` column is 1sec, so we need a delay
-			time.Sleep(time.Second)
 			updateAuthCmd := &m.UpdateAuthInfoCommand{UserId: query.Result.Id, AuthModule: "test1", AuthId: "test1"}
 			err = UpdateAuthInfo(updateAuthCmd)