deadline_test.go 732 B

12345678910111213141516171819202122232425262728293031
  1. package contextx
  2. import (
  3. "context"
  4. "testing"
  5. "time"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. func TestShrinkDeadlineLess(t *testing.T) {
  9. deadline := time.Now().Add(time.Second)
  10. ctx, cancel := context.WithDeadline(context.Background(), deadline)
  11. defer cancel()
  12. ctx, cancel = ShrinkDeadline(ctx, time.Minute)
  13. defer cancel()
  14. dl, ok := ctx.Deadline()
  15. assert.True(t, ok)
  16. assert.Equal(t, deadline, dl)
  17. }
  18. func TestShrinkDeadlineMore(t *testing.T) {
  19. deadline := time.Now().Add(time.Minute)
  20. ctx, cancel := context.WithDeadline(context.Background(), deadline)
  21. defer cancel()
  22. ctx, cancel = ShrinkDeadline(ctx, time.Second)
  23. defer cancel()
  24. dl, ok := ctx.Deadline()
  25. assert.True(t, ok)
  26. assert.True(t, dl.Before(deadline))
  27. }