file_test.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package cache
  2. import (
  3. "fmt"
  4. "reflect"
  5. "testing"
  6. )
  7. func TestFileSet(t *testing.T) {
  8. fc := NewFileCache()
  9. err := fc.Set("123", "ddd", 0)
  10. if err != nil {
  11. fmt.Println(err.Error())
  12. t.Fatalf("写入失败")
  13. }
  14. }
  15. func TestFileGet(t *testing.T) {
  16. fc := NewFileCache()
  17. res := ""
  18. err := fc.Get("123", &res)
  19. if err != nil {
  20. fmt.Println(err.Error())
  21. t.Fatalf("读取失败")
  22. }
  23. fmt.Println("res", res)
  24. }
  25. func TestFileSetGet(t *testing.T) {
  26. fc := NewFileCache()
  27. err := fc.Set("key1", "ddd", 0)
  28. res := ""
  29. err = fc.Get("key1", &res)
  30. if err != nil {
  31. fmt.Println(err.Error())
  32. t.Fatalf("读取失败")
  33. }
  34. fmt.Println("res", res)
  35. }
  36. func TestFileGetJson(t *testing.T) {
  37. fc := NewFileCache()
  38. old := &r{
  39. A: "a", B: "b",
  40. }
  41. fc.Set("123", old, 0)
  42. res := &r{}
  43. err2 := fc.Get("123", res)
  44. fmt.Println("res", res)
  45. if err2 != nil {
  46. t.Fatalf("读取失败" + err2.Error())
  47. }
  48. }
  49. func TestFileSetGetJson(t *testing.T) {
  50. fc := NewFileCache()
  51. old_rr := &rr{AA: "aa", BB: "bb"}
  52. old := &r{
  53. A: "a", B: "b",
  54. R: old_rr,
  55. }
  56. err := fc.Set("123", old, 300)
  57. if err != nil {
  58. t.Fatalf("写入失败")
  59. }
  60. //old_rr.AA = "aaa"
  61. fmt.Println("old_rr", old)
  62. res := &r{}
  63. err2 := fc.Get("123", res)
  64. fmt.Println("res", res)
  65. if err2 != nil {
  66. t.Fatalf("读取失败" + err2.Error())
  67. }
  68. if !reflect.DeepEqual(res, old) {
  69. t.Fatalf("读取错误")
  70. }
  71. }
  72. func BenchmarkSet(b *testing.B) {
  73. fc := NewFileCache()
  74. b.ResetTimer()
  75. for i := 0; i < b.N; i++ {
  76. fc.Set("123", "{dsv}", 1000)
  77. }
  78. }
  79. func BenchmarkGet(b *testing.B) {
  80. fc := NewFileCache()
  81. b.ResetTimer()
  82. v := ""
  83. for i := 0; i < b.N; i++ {
  84. fc.Get("123", &v)
  85. }
  86. }