Skip to content

Commit

Permalink
Update context_test.go
Browse files Browse the repository at this point in the history
github.com/goravel/goravel/issues/504

tests fo WithContext
  • Loading branch information
KlassnayaAfrodita authored Oct 9, 2024
1 parent 7eb0339 commit 8e36717
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,35 @@ func TestContext(t *testing.T) {
assert.Equal(t, "one", ctx.Value(1))
assert.Equal(t, "two point two", ctx.Value(2.2))
}

func TestWithContext(t *testing.T) {
httpCtx := Background()

timeoutCtx, cancel := context.WithTimeout(context.Background(), 2*time.Second)

Check failure on line 37 in context_test.go

View workflow job for this annotation

GitHub Actions / test / ubuntu (1.22)

undefined: context

Check failure on line 37 in context_test.go

View workflow job for this annotation

GitHub Actions / test / ubuntu (1.22)

undefined: time

Check failure on line 37 in context_test.go

View workflow job for this annotation

GitHub Actions / lint / lint

undefined: context

Check failure on line 37 in context_test.go

View workflow job for this annotation

GitHub Actions / lint / lint

undefined: time

Check failure on line 37 in context_test.go

View workflow job for this annotation

GitHub Actions / lint / nilaway

undefined: context

Check failure on line 37 in context_test.go

View workflow job for this annotation

GitHub Actions / lint / nilaway

undefined: context

Check failure on line 37 in context_test.go

View workflow job for this annotation

GitHub Actions / lint / nilaway

undefined: time

Check failure on line 37 in context_test.go

View workflow job for this annotation

GitHub Actions / test / ubuntu (1.23)

undefined: context

Check failure on line 37 in context_test.go

View workflow job for this annotation

GitHub Actions / test / ubuntu (1.23)

undefined: time

Check failure on line 37 in context_test.go

View workflow job for this annotation

GitHub Actions / test / windows (1.23)

undefined: context

Check failure on line 37 in context_test.go

View workflow job for this annotation

GitHub Actions / test / windows (1.23)

undefined: time

Check failure on line 37 in context_test.go

View workflow job for this annotation

GitHub Actions / codecov / codecov

undefined: context

Check failure on line 37 in context_test.go

View workflow job for this annotation

GitHub Actions / codecov / codecov

undefined: time
defer cancel()

httpCtx.WithContext(timeoutCtx)

Check failure on line 40 in context_test.go

View workflow job for this annotation

GitHub Actions / test / ubuntu (1.22)

httpCtx.WithContext undefined (type "github.com/goravel/framework/contracts/http".Context has no field or method WithContext)

Check failure on line 40 in context_test.go

View workflow job for this annotation

GitHub Actions / lint / lint

httpCtx.WithContext undefined (type "github.com/goravel/framework/contracts/http".Context has no field or method WithContext)

Check failure on line 40 in context_test.go

View workflow job for this annotation

GitHub Actions / lint / nilaway

httpCtx.WithContext undefined (type "github.com/goravel/framework/contracts/http".Context has no field or method WithContext)

Check failure on line 40 in context_test.go

View workflow job for this annotation

GitHub Actions / test / ubuntu (1.23)

httpCtx.WithContext undefined (type "github.com/goravel/framework/contracts/http".Context has no field or method WithContext)

Check failure on line 40 in context_test.go

View workflow job for this annotation

GitHub Actions / test / windows (1.23)

httpCtx.WithContext undefined (type "github.com/goravel/framework/contracts/http".Context has no field or method WithContext)

Check failure on line 40 in context_test.go

View workflow job for this annotation

GitHub Actions / codecov / codecov

httpCtx.WithContext undefined (type "github.com/goravel/framework/contracts/http".Context has no field or method WithContext)

ctx := httpCtx.Context()
assert.Equal(t, timeoutCtx, ctx)

deadline, ok := ctx.Deadline()
assert.True(t, ok, "Deadline should be set")
assert.WithinDuration(t, time.Now().Add(2*time.Second), deadline, 50*time.Millisecond, "Deadline should be approximately 2 seconds from now")

Check failure on line 47 in context_test.go

View workflow job for this annotation

GitHub Actions / test / ubuntu (1.22)

undefined: time

Check failure on line 47 in context_test.go

View workflow job for this annotation

GitHub Actions / lint / lint

undefined: time

Check failure on line 47 in context_test.go

View workflow job for this annotation

GitHub Actions / lint / nilaway

undefined: time

Check failure on line 47 in context_test.go

View workflow job for this annotation

GitHub Actions / lint / nilaway

undefined: time

Check failure on line 47 in context_test.go

View workflow job for this annotation

GitHub Actions / lint / nilaway

undefined: time

Check failure on line 47 in context_test.go

View workflow job for this annotation

GitHub Actions / test / ubuntu (1.23)

undefined: time

Check failure on line 47 in context_test.go

View workflow job for this annotation

GitHub Actions / test / windows (1.23)

undefined: time

Check failure on line 47 in context_test.go

View workflow job for this annotation

GitHub Actions / codecov / codecov

undefined: time

select {
case <-ctx.Done():
assert.Fail(t, "context should not be done yet")
default:

}

time.Sleep(2 * time.Second)

Check failure on line 56 in context_test.go

View workflow job for this annotation

GitHub Actions / test / ubuntu (1.22)

undefined: time

Check failure on line 56 in context_test.go

View workflow job for this annotation

GitHub Actions / lint / lint

undefined: time

Check failure on line 56 in context_test.go

View workflow job for this annotation

GitHub Actions / lint / nilaway

undefined: time

Check failure on line 56 in context_test.go

View workflow job for this annotation

GitHub Actions / lint / nilaway

undefined: time

Check failure on line 56 in context_test.go

View workflow job for this annotation

GitHub Actions / test / ubuntu (1.23)

undefined: time

Check failure on line 56 in context_test.go

View workflow job for this annotation

GitHub Actions / test / windows (1.23)

undefined: time

Check failure on line 56 in context_test.go

View workflow job for this annotation

GitHub Actions / codecov / codecov

undefined: time

select {
case <-ctx.Done():
assert.Equal(t, context.DeadlineExceeded, ctx.Err(), "context should be exceeded")

Check failure on line 60 in context_test.go

View workflow job for this annotation

GitHub Actions / test / ubuntu (1.22)

undefined: context

Check failure on line 60 in context_test.go

View workflow job for this annotation

GitHub Actions / lint / lint

undefined: context (typecheck)

Check failure on line 60 in context_test.go

View workflow job for this annotation

GitHub Actions / lint / nilaway

undefined: context

Check failure on line 60 in context_test.go

View workflow job for this annotation

GitHub Actions / test / ubuntu (1.23)

undefined: context

Check failure on line 60 in context_test.go

View workflow job for this annotation

GitHub Actions / test / windows (1.23)

undefined: context

Check failure on line 60 in context_test.go

View workflow job for this annotation

GitHub Actions / codecov / codecov

undefined: context
default:
assert.Fail(t, "context should be done after timeout")
}
}

0 comments on commit 8e36717

Please sign in to comment.