hbase_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package hbase
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "os"
  7. "strings"
  8. "testing"
  9. "time"
  10. "github.com/tsuna/gohbase/hrpc"
  11. xtime "go-common/library/time"
  12. )
  13. var addrs []string
  14. var client *Client
  15. func TestMain(m *testing.M) {
  16. addrsStr := os.Getenv("HBASE_TEST_ADDRS")
  17. if addrsStr == "" {
  18. println("HBASE_TEST_ADDRS not set skip test !!")
  19. return
  20. }
  21. addrs = strings.Split(addrsStr, ",")
  22. config := &Config{
  23. Zookeeper: &ZKConfig{Root: "", Addrs: addrs, Timeout: xtime.Duration(time.Second)},
  24. }
  25. client = NewClient(config)
  26. os.Exit(m.Run())
  27. }
  28. func TestPing(t *testing.T) {
  29. if err := client.Ping(context.Background()); err != nil {
  30. t.Errorf("ping meet err: %v", err)
  31. }
  32. }
  33. func TestPutGetDelete(t *testing.T) {
  34. ctx := context.Background()
  35. values := map[string]map[string][]byte{"name": {"firstname": []byte("hello"), "lastname": []byte("world")}}
  36. result, err := client.PutStr(ctx, "user", "user1", values)
  37. if err != nil {
  38. t.Fatal(err)
  39. }
  40. t.Logf("%v", result)
  41. result, err = client.GetStr(ctx, "user", "user1")
  42. if err != nil {
  43. t.Fatal(err)
  44. }
  45. if len(result.Cells) != 2 {
  46. t.Errorf("unexpect result, expect 2 cell, get %d", len(result.Cells))
  47. }
  48. _, err = client.Delete(ctx, "user", "user1", values)
  49. if err != nil {
  50. t.Fatal(err)
  51. }
  52. result, err = client.GetStr(ctx, "user", "user1")
  53. if err != nil {
  54. t.Fatal(err)
  55. }
  56. if len(result.Cells) > 0 {
  57. t.Errorf("unexpect result, found cells")
  58. }
  59. }
  60. func TestScan(t *testing.T) {
  61. N := 10
  62. ctx := context.Background()
  63. values := map[string]map[string][]byte{"name": {"firstname": []byte("hello"), "lastname": []byte("world")}}
  64. for i := 0; i < N; i++ {
  65. _, err := client.PutStr(ctx, "user", fmt.Sprintf("scan_%d", i), values)
  66. if err != nil {
  67. t.Error(err)
  68. }
  69. }
  70. results, err := client.ScanStrAll(ctx, "user")
  71. if err != nil {
  72. t.Fatal(err)
  73. }
  74. if len(results) != N {
  75. t.Errorf("unexpect result expect %d result get %d", N, len(results))
  76. }
  77. iter, err := client.ScanStr(ctx, "user")
  78. if err != nil {
  79. t.Fatal(err)
  80. }
  81. defer iter.Close()
  82. GN := 0
  83. for {
  84. _, err := iter.Next()
  85. if err != nil {
  86. if err == io.EOF {
  87. break
  88. }
  89. t.Error(err)
  90. }
  91. GN++
  92. }
  93. if GN != N {
  94. t.Errorf("unexpect result expect %d result get %d", N, GN)
  95. }
  96. for i := 0; i < N; i++ {
  97. _, err := client.Delete(ctx, "user", fmt.Sprintf("scan_%d", i), nil)
  98. if err != nil {
  99. t.Errorf("delete error %s", err)
  100. }
  101. }
  102. }
  103. func TestScanRange(t *testing.T) {
  104. N := 10
  105. ctx := context.Background()
  106. values := map[string]map[string][]byte{"name": {"firstname": []byte("hello"), "lastname": []byte("world")}}
  107. for i := 0; i < N; i++ {
  108. _, err := client.PutStr(ctx, "user", fmt.Sprintf("scan_%d", i), values)
  109. if err != nil {
  110. t.Error(err)
  111. }
  112. }
  113. scanner, err := client.ScanRangeStr(ctx, "user", "scan_0", "scan_3")
  114. if err != nil {
  115. t.Fatal(err)
  116. }
  117. var results []*hrpc.Result
  118. for {
  119. result, err := scanner.Next()
  120. if err != nil {
  121. if err == io.EOF {
  122. break
  123. }
  124. t.Fatal(err)
  125. }
  126. results = append(results, result)
  127. }
  128. if len(results) != 3 {
  129. t.Errorf("unexpect result expect %d result get %d", N, len(results))
  130. }
  131. for i := 0; i < N; i++ {
  132. _, err := client.Delete(ctx, "user", fmt.Sprintf("scan_%d", i), nil)
  133. if err != nil {
  134. t.Errorf("delete error %s", err)
  135. }
  136. }
  137. }
  138. func TestClose(t *testing.T) {
  139. if err := client.Close(); err != nil {
  140. t.Logf("Close meet error: %v", err)
  141. }
  142. if err := client.Ping(context.Background()); err == nil {
  143. t.Errorf("ping return nil error after being closed")
  144. }
  145. }