You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

discovery_cache_test.go 1.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package openid
  4. import (
  5. "testing"
  6. "time"
  7. )
  8. type testDiscoveredInfo struct{}
  9. func (s *testDiscoveredInfo) ClaimedID() string {
  10. return "claimedID"
  11. }
  12. func (s *testDiscoveredInfo) OpEndpoint() string {
  13. return "opEndpoint"
  14. }
  15. func (s *testDiscoveredInfo) OpLocalID() string {
  16. return "opLocalID"
  17. }
  18. func TestTimedDiscoveryCache(t *testing.T) {
  19. dc := newTimedDiscoveryCache(1 * time.Second)
  20. // Put some initial values
  21. dc.Put("foo", &testDiscoveredInfo{}) // openid.opEndpoint: "a", openid.opLocalID: "b", openid.claimedID: "c"})
  22. // Make sure we can retrieve them
  23. if di := dc.Get("foo"); di == nil {
  24. t.Errorf("Expected a result, got nil")
  25. } else if di.OpEndpoint() != "opEndpoint" || di.OpLocalID() != "opLocalID" || di.ClaimedID() != "claimedID" {
  26. t.Errorf("Expected opEndpoint opLocalID claimedID, got %v %v %v", di.OpEndpoint(), di.OpLocalID(), di.ClaimedID())
  27. }
  28. // Attempt to get a non-existent value
  29. if di := dc.Get("bar"); di != nil {
  30. t.Errorf("Expected nil, got %v", di)
  31. }
  32. // Sleep one second and try retrieve again
  33. time.Sleep(1 * time.Second)
  34. if di := dc.Get("foo"); di != nil {
  35. t.Errorf("Expected a nil, got a result")
  36. }
  37. }