state_test.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. package test
  2. import (
  3. "encoding/json"
  4. "git.sxidc.com/service-supports/dapr_api/state"
  5. "git.sxidc.com/service-supports/dapr_api/utils"
  6. "github.com/stretchr/testify/assert"
  7. "testing"
  8. "time"
  9. )
  10. func TestStateSaveAndGet(t *testing.T) {
  11. api := state.NewAPI(daprHttpPort, 10*time.Second)
  12. defer state.DestroyAPI(api)
  13. key := utils.SimpleUUID()
  14. value := utils.SimpleUUID()
  15. err := api.SaveState(stateStoreName, []state.SaveStateRequest{
  16. {
  17. Key: key,
  18. Value: value,
  19. },
  20. })
  21. if err != nil {
  22. t.Fatal(err)
  23. }
  24. data, etag, err := api.GetState(stateStoreName, key, nil)
  25. if err != nil {
  26. t.Fatal(err)
  27. }
  28. assert.NotEmpty(t, etag, "etag为空")
  29. assert.Equal(t, value, data, "value不一致")
  30. err = api.DeleteState(stateStoreName, key, nil)
  31. if err != nil {
  32. t.Fatal(err)
  33. }
  34. data, etag, err = api.GetState(stateStoreName, key, nil)
  35. if err != nil {
  36. t.Fatal(err)
  37. }
  38. assert.Empty(t, etag, "etag不为空")
  39. assert.Empty(t, data, "value不为空")
  40. }
  41. func TestStateSaveAndGetJson(t *testing.T) {
  42. api := state.NewAPI(daprHttpPort, 10*time.Second)
  43. defer state.DestroyAPI(api)
  44. key := utils.SimpleUUID()
  45. value, err := json.Marshal(map[string]interface{}{"tree": map[string]interface{}{"value": "aaa"}})
  46. if err != nil {
  47. t.Fatal(err)
  48. }
  49. err = api.SaveState(stateStoreName, []state.SaveStateRequest{
  50. {
  51. Key: key,
  52. Value: string(value),
  53. },
  54. })
  55. if err != nil {
  56. t.Fatal(err)
  57. }
  58. data, etag, err := api.GetState(stateStoreName, key, nil)
  59. if err != nil {
  60. t.Fatal(err)
  61. }
  62. assert.NotEmpty(t, etag, "etag为空")
  63. assert.Equal(t, string(value), data, "value不一致")
  64. err = api.DeleteState(stateStoreName, key, nil)
  65. if err != nil {
  66. t.Fatal(err)
  67. }
  68. data, etag, err = api.GetState(stateStoreName, key, nil)
  69. if err != nil {
  70. t.Fatal(err)
  71. }
  72. assert.Empty(t, etag, "etag不为空")
  73. assert.Empty(t, string(data), "value不为空")
  74. }
  75. func TestStateGetBulk(t *testing.T) {
  76. api := state.NewAPI(daprHttpPort, 10*time.Second)
  77. defer state.DestroyAPI(api)
  78. key1 := utils.SimpleUUID()
  79. value1 := utils.SimpleUUID()
  80. key2 := utils.SimpleUUID()
  81. value2 := utils.SimpleUUID()
  82. err := api.SaveState(stateStoreName, []state.SaveStateRequest{
  83. {
  84. Key: key1,
  85. Value: value1,
  86. },
  87. {
  88. Key: key2,
  89. Value: value2,
  90. },
  91. })
  92. if err != nil {
  93. t.Fatal(err)
  94. }
  95. getStateBulkItems, err := api.GetStateBulk(stateStoreName, nil, state.GetStateBulkRequest{
  96. Keys: []string{key1, key2},
  97. Parallelism: 1,
  98. })
  99. if err != nil {
  100. t.Fatal(err)
  101. }
  102. assert.Equal(t, 2, len(getStateBulkItems), "获取到的state数量不正确")
  103. for _, getStateBulkItem := range getStateBulkItems {
  104. assert.NotEmpty(t, getStateBulkItem.Etag, "etag为空")
  105. if getStateBulkItem.Key != key1 && getStateBulkItem.Key != key2 {
  106. t.Fatal("批量获取到的key错误")
  107. }
  108. if getStateBulkItem.Key == key1 {
  109. assert.Equal(t, value1, getStateBulkItem.Data, "value不一致")
  110. }
  111. if getStateBulkItem.Key == key2 {
  112. assert.Equal(t, value2, getStateBulkItem.Data, "value不一致")
  113. }
  114. }
  115. err = api.DeleteState(stateStoreName, key2, nil)
  116. if err != nil {
  117. t.Fatal(err)
  118. }
  119. err = api.DeleteState(stateStoreName, key1, nil)
  120. if err != nil {
  121. t.Fatal(err)
  122. }
  123. getStateBulkItems, err = api.GetStateBulk(stateStoreName, nil, state.GetStateBulkRequest{
  124. Keys: []string{key1, key2},
  125. Parallelism: 1,
  126. })
  127. if err != nil {
  128. t.Fatal(err)
  129. }
  130. for _, getStateBulkItem := range getStateBulkItems {
  131. assert.Empty(t, getStateBulkItem.Etag, "获取到的etag不为空")
  132. assert.Empty(t, getStateBulkItem.Data, "获取到的data不为空")
  133. }
  134. }
  135. func TestTransaction(t *testing.T) {
  136. api := state.NewAPI(daprHttpPort, 10*time.Second)
  137. defer state.DestroyAPI(api)
  138. key1 := utils.SimpleUUID()
  139. value1 := utils.SimpleUUID()
  140. key2 := utils.SimpleUUID()
  141. value2 := utils.SimpleUUID()
  142. err := api.Transaction(stateStoreName, state.TransactionRequest{
  143. Operations: []state.TransactionOperation{
  144. {
  145. Operation: state.TransactionUpsert,
  146. Request: state.TransactionOperationRequest{
  147. Key: key1,
  148. Value: value1,
  149. },
  150. },
  151. {
  152. Operation: state.TransactionUpsert,
  153. Request: state.TransactionOperationRequest{
  154. Key: key2,
  155. Value: value2,
  156. },
  157. },
  158. },
  159. })
  160. if err != nil {
  161. t.Fatal(err)
  162. }
  163. getStateBulkItems, err := api.GetStateBulk(stateStoreName, nil, state.GetStateBulkRequest{
  164. Keys: []string{key1, key2},
  165. Parallelism: 1,
  166. })
  167. if err != nil {
  168. t.Fatal(err)
  169. }
  170. assert.Equal(t, 2, len(getStateBulkItems), "获取到的state数量不正确")
  171. for _, getStateBulkItem := range getStateBulkItems {
  172. assert.NotEmpty(t, getStateBulkItem.Etag, "etag为空")
  173. if getStateBulkItem.Key != key1 && getStateBulkItem.Key != key2 {
  174. t.Fatal("批量获取到的key错误")
  175. }
  176. if getStateBulkItem.Key == key1 {
  177. assert.Equal(t, value1, getStateBulkItem.Data, "value不一致")
  178. }
  179. if getStateBulkItem.Key == key2 {
  180. assert.Equal(t, value2, getStateBulkItem.Data, "value不一致")
  181. }
  182. }
  183. err = api.Transaction(stateStoreName, state.TransactionRequest{
  184. Operations: []state.TransactionOperation{
  185. {
  186. Operation: state.TransactionDelete,
  187. Request: state.TransactionOperationRequest{
  188. Key: key1,
  189. },
  190. },
  191. {
  192. Operation: state.TransactionDelete,
  193. Request: state.TransactionOperationRequest{
  194. Key: key2,
  195. },
  196. },
  197. },
  198. })
  199. if err != nil {
  200. t.Fatal(err)
  201. }
  202. getStateBulkItems, err = api.GetStateBulk(stateStoreName, nil, state.GetStateBulkRequest{
  203. Keys: []string{key1, key2},
  204. Parallelism: 1,
  205. })
  206. if err != nil {
  207. t.Fatal(err)
  208. }
  209. for _, getStateBulkItem := range getStateBulkItems {
  210. assert.Empty(t, getStateBulkItem.Etag, "获取到的etag不为空")
  211. assert.Empty(t, getStateBulkItem.Data, "获取到的data不为空")
  212. }
  213. }