2
0

kvmanager_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package kvmanager
  2. import "testing"
  3. func TestParseKVAddr(t *testing.T) {
  4. tests := []struct {
  5. name string
  6. input string
  7. wantNetwork string
  8. wantTarget string
  9. }{
  10. {
  11. name: "tcp address",
  12. input: "localhost:8085",
  13. wantNetwork: "tcp",
  14. wantTarget: "localhost:8085",
  15. },
  16. {
  17. name: "unix socket",
  18. input: "unix:.pizzakv.sock",
  19. wantNetwork: "unix",
  20. wantTarget: ".pizzakv.sock",
  21. },
  22. }
  23. for _, tt := range tests {
  24. t.Run(tt.name, func(t *testing.T) {
  25. network, target := parseKVAddr(tt.input)
  26. if network != tt.wantNetwork {
  27. t.Errorf("Expected network %q, got %q", tt.wantNetwork, network)
  28. }
  29. if target != tt.wantTarget {
  30. t.Errorf("Expected target %q, got %q", tt.wantTarget, target)
  31. }
  32. })
  33. }
  34. }
  35. func TestParseFlags(t *testing.T) {
  36. tests := []struct {
  37. name string
  38. input string
  39. expected []string
  40. }{
  41. {
  42. name: "empty string",
  43. input: "",
  44. expected: nil,
  45. },
  46. {
  47. name: "single flag",
  48. input: "-iwal",
  49. expected: []string{"-iwal"},
  50. },
  51. {
  52. name: "multiple flags",
  53. input: "-iwal -port=9090",
  54. expected: []string{"-iwal", "-port=9090"},
  55. },
  56. {
  57. name: "flags with quotes",
  58. input: "-path=\"/tmp/my path\" -verbose",
  59. expected: []string{"-path=/tmp/my path", "-verbose"},
  60. },
  61. {
  62. name: "flags with extra spaces",
  63. input: " -iwal -port=9090 ",
  64. expected: []string{"-iwal", "-port=9090"},
  65. },
  66. }
  67. for _, tt := range tests {
  68. t.Run(tt.name, func(t *testing.T) {
  69. result := parseFlags(tt.input)
  70. if len(result) != len(tt.expected) {
  71. t.Errorf("Expected %d args, got %d", len(tt.expected), len(result))
  72. return
  73. }
  74. for i := range result {
  75. if result[i] != tt.expected[i] {
  76. t.Errorf("Arg %d: expected %q, got %q", i, tt.expected[i], result[i])
  77. }
  78. }
  79. })
  80. }
  81. }
  82. func TestParsePort(t *testing.T) {
  83. tests := []struct {
  84. name string
  85. input string
  86. expected int
  87. wantErr bool
  88. }{
  89. {
  90. name: "valid address",
  91. input: "localhost:8085",
  92. expected: 8085,
  93. wantErr: false,
  94. },
  95. {
  96. name: "valid IP address",
  97. input: "127.0.0.1:9090",
  98. expected: 9090,
  99. wantErr: false,
  100. },
  101. {
  102. name: "invalid format",
  103. input: "localhost",
  104. expected: 0,
  105. wantErr: true,
  106. },
  107. {
  108. name: "invalid port",
  109. input: "localhost:abc",
  110. expected: 0,
  111. wantErr: true,
  112. },
  113. }
  114. for _, tt := range tests {
  115. t.Run(tt.name, func(t *testing.T) {
  116. result, err := ParsePort(tt.input)
  117. if tt.wantErr {
  118. if err == nil {
  119. t.Error("Expected error but got none")
  120. }
  121. return
  122. }
  123. if err != nil {
  124. t.Errorf("Unexpected error: %v", err)
  125. return
  126. }
  127. if result != tt.expected {
  128. t.Errorf("Expected port %d, got %d", tt.expected, result)
  129. }
  130. })
  131. }
  132. }
  133. func TestKVInfo(t *testing.T) {
  134. info := &KVInfo{
  135. PID: 12345,
  136. Addr: "unix:.pizzakv.sock",
  137. }
  138. if info.PID != 12345 {
  139. t.Errorf("Expected PID 12345, got %d", info.PID)
  140. }
  141. if info.Addr != "unix:.pizzakv.sock" {
  142. t.Errorf("Expected Addr 'unix:.pizzakv.sock', got %s", info.Addr)
  143. }
  144. }