From 25514fb553c1ec19c16205acc6d80f6b6b9dcdf4 Mon Sep 17 00:00:00 2001 From: Todd Knight Date: Mon, 27 Jul 2020 14:06:59 -0700 Subject: [PATCH] Fix a failing test due to time resolutions mismatch. (#221) * Round generated expiration timestamps to the nearest second. --- internal/authtoken/repository.go | 4 +++- internal/authtoken/repository_test.go | 5 ++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/internal/authtoken/repository.go b/internal/authtoken/repository.go index 1f8b526f1..320311ad4 100644 --- a/internal/authtoken/repository.go +++ b/internal/authtoken/repository.go @@ -86,7 +86,9 @@ func (r *Repository) CreateAuthToken(ctx context.Context, withIamUserId, withAut at.Token = token // TODO: Allow the caller to specify something different than the default duration. - expiration, err := ptypes.TimestampProto(time.Now().Add(maxTokenDuration)) + // We round the expiration time to the nearest second to make testing in different platforms with + // different time resolutions easier. + expiration, err := ptypes.TimestampProto(time.Now().Round(time.Second).Add(maxTokenDuration)) if err != nil { return nil, err } diff --git a/internal/authtoken/repository_test.go b/internal/authtoken/repository_test.go index 465030325..c8802e336 100644 --- a/internal/authtoken/repository_test.go +++ b/internal/authtoken/repository_test.go @@ -3,6 +3,7 @@ package authtoken import ( "context" "errors" + "sort" "testing" "time" @@ -575,7 +576,9 @@ func TestRepository_ListAuthTokens(t *testing.T) { return } assert.NoError(err) - assert.ElementsMatchf(tt.want, got, "row count") + sort.Slice(tt.want, func(i, j int) bool { return tt.want[i].PublicId < tt.want[j].PublicId }) + sort.Slice(got, func(i, j int) bool { return got[i].PublicId < got[j].PublicId }) + assert.Empty(cmp.Diff(tt.want, got, protocmp.Transform()), "row count") }) } }