session_test.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. // +build all integration
  2. package gocql
  3. import (
  4. "context"
  5. "fmt"
  6. "testing"
  7. )
  8. func TestSessionAPI(t *testing.T) {
  9. cfg := &ClusterConfig{}
  10. s := &Session{
  11. cfg: *cfg,
  12. cons: Quorum,
  13. policy: RoundRobinHostPolicy(),
  14. }
  15. s.pool = cfg.PoolConfig.buildPool(s)
  16. s.executor = &queryExecutor{
  17. pool: s.pool,
  18. policy: s.policy,
  19. }
  20. defer s.Close()
  21. s.SetConsistency(All)
  22. if s.cons != All {
  23. t.Fatalf("expected consistency 'All', got '%v'", s.cons)
  24. }
  25. s.SetPageSize(100)
  26. if s.pageSize != 100 {
  27. t.Fatalf("expected pageSize 100, got %v", s.pageSize)
  28. }
  29. s.SetPrefetch(0.75)
  30. if s.prefetch != 0.75 {
  31. t.Fatalf("expceted prefetch 0.75, got %v", s.prefetch)
  32. }
  33. trace := &traceWriter{}
  34. s.SetTrace(trace)
  35. if s.trace != trace {
  36. t.Fatalf("expected traceWriter '%v',got '%v'", trace, s.trace)
  37. }
  38. qry := s.Query("test", 1)
  39. if v, ok := qry.values[0].(int); !ok {
  40. t.Fatalf("expected qry.values[0] to be an int, got %v", qry.values[0])
  41. } else if v != 1 {
  42. t.Fatalf("expceted qry.values[0] to be 1, got %v", v)
  43. } else if qry.stmt != "test" {
  44. t.Fatalf("expected qry.stmt to be 'test', got '%v'", qry.stmt)
  45. }
  46. boundQry := s.Bind("test", func(q *QueryInfo) ([]interface{}, error) {
  47. return nil, nil
  48. })
  49. if boundQry.binding == nil {
  50. t.Fatal("expected qry.binding to be defined, got nil")
  51. } else if boundQry.stmt != "test" {
  52. t.Fatalf("expected qry.stmt to be 'test', got '%v'", boundQry.stmt)
  53. }
  54. itr := s.executeQuery(qry)
  55. if itr.err != ErrNoConnections {
  56. t.Fatalf("expected itr.err to be '%v', got '%v'", ErrNoConnections, itr.err)
  57. }
  58. testBatch := s.NewBatch(LoggedBatch)
  59. testBatch.Query("test")
  60. err := s.ExecuteBatch(testBatch)
  61. if err != ErrNoConnections {
  62. t.Fatalf("expected session.ExecuteBatch to return '%v', got '%v'", ErrNoConnections, err)
  63. }
  64. s.Close()
  65. if !s.Closed() {
  66. t.Fatal("expected s.Closed() to be true, got false")
  67. }
  68. //Should just return cleanly
  69. s.Close()
  70. err = s.ExecuteBatch(testBatch)
  71. if err != ErrSessionClosed {
  72. t.Fatalf("expected session.ExecuteBatch to return '%v', got '%v'", ErrSessionClosed, err)
  73. }
  74. }
  75. type funcQueryObserver func(context.Context, ObservedQuery)
  76. func (f funcQueryObserver) ObserveQuery(ctx context.Context, o ObservedQuery) {
  77. f(ctx, o)
  78. }
  79. func TestQueryBasicAPI(t *testing.T) {
  80. qry := &Query{}
  81. if qry.Latency() != 0 {
  82. t.Fatalf("expected Query.Latency() to return 0, got %v", qry.Latency())
  83. }
  84. qry.attempts = 2
  85. qry.totalLatency = 4
  86. if qry.Attempts() != 2 {
  87. t.Fatalf("expected Query.Attempts() to return 2, got %v", qry.Attempts())
  88. }
  89. if qry.Latency() != 2 {
  90. t.Fatalf("expected Query.Latency() to return 2, got %v", qry.Latency())
  91. }
  92. qry.Consistency(All)
  93. if qry.GetConsistency() != All {
  94. t.Fatalf("expected Query.GetConsistency to return 'All', got '%s'", qry.GetConsistency())
  95. }
  96. trace := &traceWriter{}
  97. qry.Trace(trace)
  98. if qry.trace != trace {
  99. t.Fatalf("expected Query.Trace to be '%v', got '%v'", trace, qry.trace)
  100. }
  101. observer := funcQueryObserver(func(context.Context, ObservedQuery) {})
  102. qry.Observer(observer)
  103. if qry.observer == nil { // can't compare func to func, checking not nil instead
  104. t.Fatal("expected Query.QueryObserver to be set, got nil")
  105. }
  106. qry.PageSize(10)
  107. if qry.pageSize != 10 {
  108. t.Fatalf("expected Query.PageSize to be 10, got %v", qry.pageSize)
  109. }
  110. qry.Prefetch(0.75)
  111. if qry.prefetch != 0.75 {
  112. t.Fatalf("expected Query.Prefetch to be 0.75, got %v", qry.prefetch)
  113. }
  114. rt := &SimpleRetryPolicy{NumRetries: 3}
  115. if qry.RetryPolicy(rt); qry.rt != rt {
  116. t.Fatalf("expected Query.RetryPolicy to be '%v', got '%v'", rt, qry.rt)
  117. }
  118. qry.Bind(qry)
  119. if qry.values[0] != qry {
  120. t.Fatalf("expected Query.Values[0] to be '%v', got '%v'", qry, qry.values[0])
  121. }
  122. }
  123. func TestQueryShouldPrepare(t *testing.T) {
  124. toPrepare := []string{"select * ", "INSERT INTO", "update table", "delete from", "begin batch"}
  125. cantPrepare := []string{"create table", "USE table", "LIST keyspaces", "alter table", "drop table", "grant user", "revoke user"}
  126. q := &Query{}
  127. for i := 0; i < len(toPrepare); i++ {
  128. q.stmt = toPrepare[i]
  129. if !q.shouldPrepare() {
  130. t.Fatalf("expected Query.shouldPrepare to return true, got false for statement '%v'", toPrepare[i])
  131. }
  132. }
  133. for i := 0; i < len(cantPrepare); i++ {
  134. q.stmt = cantPrepare[i]
  135. if q.shouldPrepare() {
  136. t.Fatalf("expected Query.shouldPrepare to return false, got true for statement '%v'", cantPrepare[i])
  137. }
  138. }
  139. }
  140. func TestBatchBasicAPI(t *testing.T) {
  141. cfg := &ClusterConfig{RetryPolicy: &SimpleRetryPolicy{NumRetries: 2}}
  142. s := &Session{
  143. cfg: *cfg,
  144. cons: Quorum,
  145. }
  146. defer s.Close()
  147. s.pool = cfg.PoolConfig.buildPool(s)
  148. // Test UnloggedBatch
  149. b := s.NewBatch(UnloggedBatch)
  150. if b.Type != UnloggedBatch {
  151. t.Fatalf("expceted batch.Type to be '%v', got '%v'", UnloggedBatch, b.Type)
  152. } else if b.rt != cfg.RetryPolicy {
  153. t.Fatalf("expceted batch.RetryPolicy to be '%v', got '%v'", cfg.RetryPolicy, b.rt)
  154. }
  155. // Test LoggedBatch
  156. b = NewBatch(LoggedBatch)
  157. if b.Type != LoggedBatch {
  158. t.Fatalf("expected batch.Type to be '%v', got '%v'", LoggedBatch, b.Type)
  159. }
  160. // Test attempts
  161. b.attempts = 1
  162. if b.Attempts() != 1 {
  163. t.Fatalf("expceted batch.Attempts() to return %v, got %v", 1, b.Attempts())
  164. }
  165. // Test latency
  166. if b.Latency() != 0 {
  167. t.Fatalf("expected batch.Latency() to be 0, got %v", b.Latency())
  168. }
  169. b.totalLatency = 4
  170. if b.Latency() != 4 {
  171. t.Fatalf("expected batch.Latency() to return %v, got %v", 4, b.Latency())
  172. }
  173. // Test Consistency
  174. b.Cons = One
  175. if b.GetConsistency() != One {
  176. t.Fatalf("expected batch.GetConsistency() to return 'One', got '%s'", b.GetConsistency())
  177. }
  178. // Test batch.Query()
  179. b.Query("test", 1)
  180. if b.Entries[0].Stmt != "test" {
  181. t.Fatalf("expected batch.Entries[0].Statement to be 'test', got '%v'", b.Entries[0].Stmt)
  182. } else if b.Entries[0].Args[0].(int) != 1 {
  183. t.Fatalf("expected batch.Entries[0].Args[0] to be 1, got %v", b.Entries[0].Args[0])
  184. }
  185. b.Bind("test2", func(q *QueryInfo) ([]interface{}, error) {
  186. return nil, nil
  187. })
  188. if b.Entries[1].Stmt != "test2" {
  189. t.Fatalf("expected batch.Entries[1].Statement to be 'test2', got '%v'", b.Entries[1].Stmt)
  190. } else if b.Entries[1].binding == nil {
  191. t.Fatal("expected batch.Entries[1].binding to be defined, got nil")
  192. }
  193. // Test RetryPolicy
  194. r := &SimpleRetryPolicy{NumRetries: 4}
  195. b.RetryPolicy(r)
  196. if b.rt != r {
  197. t.Fatalf("expected batch.RetryPolicy to be '%v', got '%v'", r, b.rt)
  198. }
  199. if b.Size() != 2 {
  200. t.Fatalf("expected batch.Size() to return 2, got %v", b.Size())
  201. }
  202. }
  203. func TestConsistencyNames(t *testing.T) {
  204. names := map[fmt.Stringer]string{
  205. Any: "ANY",
  206. One: "ONE",
  207. Two: "TWO",
  208. Three: "THREE",
  209. Quorum: "QUORUM",
  210. All: "ALL",
  211. LocalQuorum: "LOCAL_QUORUM",
  212. EachQuorum: "EACH_QUORUM",
  213. Serial: "SERIAL",
  214. LocalSerial: "LOCAL_SERIAL",
  215. LocalOne: "LOCAL_ONE",
  216. }
  217. for k, v := range names {
  218. if k.String() != v {
  219. t.Fatalf("expected '%v', got '%v'", v, k.String())
  220. }
  221. }
  222. }
  223. func TestIsUseStatement(t *testing.T) {
  224. testCases := []struct {
  225. input string
  226. exp bool
  227. }{
  228. {"USE foo", true},
  229. {"USe foo", true},
  230. {"UsE foo", true},
  231. {"Use foo", true},
  232. {"uSE foo", true},
  233. {"uSe foo", true},
  234. {"usE foo", true},
  235. {"use foo", true},
  236. {"SELECT ", false},
  237. {"UPDATE ", false},
  238. {"INSERT ", false},
  239. {"", false},
  240. }
  241. for _, tc := range testCases {
  242. v := isUseStatement(tc.input)
  243. if v != tc.exp {
  244. t.Fatalf("expected %v but got %v for statement %q", tc.exp, v, tc.input)
  245. }
  246. }
  247. }