gitea源码

discovery_cache_test.go 1.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package openid
  4. import (
  5. "testing"
  6. "time"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/stretchr/testify/require"
  9. )
  10. type testDiscoveredInfo struct{}
  11. func (s *testDiscoveredInfo) ClaimedID() string {
  12. return "claimedID"
  13. }
  14. func (s *testDiscoveredInfo) OpEndpoint() string {
  15. return "opEndpoint"
  16. }
  17. func (s *testDiscoveredInfo) OpLocalID() string {
  18. return "opLocalID"
  19. }
  20. func TestTimedDiscoveryCache(t *testing.T) {
  21. ttl := 50 * time.Millisecond
  22. dc := newTimedDiscoveryCache(ttl)
  23. // Put some initial values
  24. dc.Put("foo", &testDiscoveredInfo{}) // openid.opEndpoint: "a", openid.opLocalID: "b", openid.claimedID: "c"})
  25. // Make sure we can retrieve them
  26. di := dc.Get("foo")
  27. require.NotNil(t, di)
  28. assert.Equal(t, "opEndpoint", di.OpEndpoint())
  29. assert.Equal(t, "opLocalID", di.OpLocalID())
  30. assert.Equal(t, "claimedID", di.ClaimedID())
  31. // Attempt to get a non-existent value
  32. assert.Nil(t, dc.Get("bar"))
  33. // Sleep for a while and try to retrieve again
  34. time.Sleep(ttl * 3 / 2)
  35. assert.Nil(t, dc.Get("foo"))
  36. }