memory_test.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package cache
  2. import (
  3. "sync"
  4. "testing"
  5. "time"
  6. )
  7. func TestMemory_Get(t *testing.T) {
  8. type fields struct {
  9. items *sync.Map
  10. queue *sync.Map
  11. wait sync.WaitGroup
  12. mutex sync.RWMutex
  13. PoolNum uint
  14. }
  15. type args struct {
  16. key string
  17. value string
  18. expire int
  19. }
  20. tests := []struct {
  21. name string
  22. fields fields
  23. args args
  24. want string
  25. wantErr bool
  26. }{
  27. {
  28. "test01",
  29. fields{},
  30. args{
  31. key: "test",
  32. value: "test",
  33. expire: 10,
  34. },
  35. "test",
  36. false,
  37. },
  38. {
  39. "test02",
  40. fields{},
  41. args{
  42. key: "test",
  43. value: "test1",
  44. expire: 1,
  45. },
  46. "",
  47. true,
  48. },
  49. }
  50. for _, tt := range tests {
  51. t.Run(tt.name, func(t *testing.T) {
  52. m := NewMemory()
  53. if err := m.Set(tt.args.key, tt.args.value, tt.args.expire); err != nil {
  54. t.Errorf("Set() error = %v", err)
  55. return
  56. }
  57. time.Sleep(2 * time.Second)
  58. got, err := m.Get(tt.args.key)
  59. if (err != nil) != tt.wantErr {
  60. t.Errorf("Get() error = %v, wantErr %v", err, tt.wantErr)
  61. return
  62. }
  63. if got != tt.want {
  64. t.Errorf("Get() got = %v, want %v", got, tt.want)
  65. }
  66. })
  67. }
  68. }