cassandra_test.go 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077
  1. // +build all integration
  2. package gocql
  3. import (
  4. "bytes"
  5. "flag"
  6. "fmt"
  7. "log"
  8. "math"
  9. "math/big"
  10. "net"
  11. "reflect"
  12. "strconv"
  13. "strings"
  14. "sync"
  15. "testing"
  16. "time"
  17. "unicode"
  18. "gopkg.in/inf.v0"
  19. )
  20. var (
  21. flagCluster = flag.String("cluster", "127.0.0.1", "a comma-separated list of host:port tuples")
  22. flagProto = flag.Int("proto", 2, "protcol version")
  23. flagCQL = flag.String("cql", "3.0.0", "CQL version")
  24. flagRF = flag.Int("rf", 1, "replication factor for test keyspace")
  25. clusterSize = flag.Int("clusterSize", 1, "the expected size of the cluster")
  26. flagRetry = flag.Int("retries", 5, "number of times to retry queries")
  27. flagAutoWait = flag.Duration("autowait", 1000*time.Millisecond, "time to wait for autodiscovery to fill the hosts poll")
  28. flagRunSslTest = flag.Bool("runssl", false, "Set to true to run ssl test")
  29. flagRunAuthTest = flag.Bool("runauth", false, "Set to true to run authentication test")
  30. flagCompressTest = flag.String("compressor", "", "compressor to use")
  31. clusterHosts []string
  32. )
  33. func init() {
  34. flag.Parse()
  35. clusterHosts = strings.Split(*flagCluster, ",")
  36. log.SetFlags(log.Lshortfile | log.LstdFlags)
  37. }
  38. func addSslOptions(cluster *ClusterConfig) *ClusterConfig {
  39. if *flagRunSslTest {
  40. cluster.SslOpts = &SslOptions{
  41. CertPath: "testdata/pki/gocql.crt",
  42. KeyPath: "testdata/pki/gocql.key",
  43. CaPath: "testdata/pki/ca.crt",
  44. EnableHostVerification: false,
  45. }
  46. }
  47. return cluster
  48. }
  49. var initOnce sync.Once
  50. func createTable(s *Session, table string) error {
  51. err := s.Query(table).Consistency(All).Exec()
  52. if *clusterSize > 1 {
  53. // wait for table definition to propogate
  54. time.Sleep(1 * time.Second)
  55. }
  56. return err
  57. }
  58. func createCluster() *ClusterConfig {
  59. cluster := NewCluster(clusterHosts...)
  60. cluster.ProtoVersion = *flagProto
  61. cluster.CQLVersion = *flagCQL
  62. cluster.Timeout = 5 * time.Second
  63. cluster.Consistency = Quorum
  64. if *flagRetry > 0 {
  65. cluster.RetryPolicy = &SimpleRetryPolicy{NumRetries: *flagRetry}
  66. }
  67. switch *flagCompressTest {
  68. case "snappy":
  69. cluster.Compressor = &SnappyCompressor{}
  70. case "":
  71. default:
  72. panic("invalid compressor: " + *flagCompressTest)
  73. }
  74. cluster = addSslOptions(cluster)
  75. return cluster
  76. }
  77. func createKeyspace(tb testing.TB, cluster *ClusterConfig, keyspace string) {
  78. c := *cluster
  79. c.Keyspace = "system"
  80. session, err := c.CreateSession()
  81. if err != nil {
  82. tb.Fatal("createSession:", err)
  83. }
  84. // should reuse the same conn apparently
  85. conn := session.Pool.Pick(nil)
  86. if conn == nil {
  87. tb.Fatal("no connections available in the pool")
  88. }
  89. err = conn.executeQuery(session.Query(`DROP KEYSPACE IF EXISTS ` + keyspace).Consistency(All)).Close()
  90. if err != nil {
  91. tb.Fatal(err)
  92. }
  93. query := session.Query(fmt.Sprintf(`CREATE KEYSPACE %s
  94. WITH replication = {
  95. 'class' : 'SimpleStrategy',
  96. 'replication_factor' : %d
  97. }`, keyspace, *flagRF)).Consistency(All)
  98. if err = conn.executeQuery(query).Close(); err != nil {
  99. tb.Fatal(err)
  100. }
  101. tb.Logf("Created keyspace %s", keyspace)
  102. }
  103. func createSessionFromCluster(cluster *ClusterConfig, tb testing.TB) *Session {
  104. // Drop and re-create the keyspace once. Different tests should use their own
  105. // individual tables, but can assume that the table does not exist before.
  106. initOnce.Do(func() {
  107. createKeyspace(tb, cluster, "gocql_test")
  108. })
  109. cluster.Keyspace = "gocql_test"
  110. session, err := cluster.CreateSession()
  111. if err != nil {
  112. tb.Fatal("createSession:", err)
  113. }
  114. return session
  115. }
  116. func createSession(tb testing.TB) *Session {
  117. cluster := createCluster()
  118. return createSessionFromCluster(cluster, tb)
  119. }
  120. // TestAuthentication verifies that gocql will work with a host configured to only accept authenticated connections
  121. func TestAuthentication(t *testing.T) {
  122. if *flagProto < 2 {
  123. t.Skip("Authentication is not supported with protocol < 2")
  124. }
  125. if !*flagRunAuthTest {
  126. t.Skip("Authentication is not configured in the target cluster")
  127. }
  128. cluster := createCluster()
  129. cluster.Authenticator = PasswordAuthenticator{
  130. Username: "cassandra",
  131. Password: "cassandra",
  132. }
  133. session, err := cluster.CreateSession()
  134. if err != nil {
  135. t.Fatalf("Authentication error: %s", err)
  136. }
  137. session.Close()
  138. }
  139. //TestRingDiscovery makes sure that you can autodiscover other cluster members when you seed a cluster config with just one node
  140. func TestRingDiscovery(t *testing.T) {
  141. cluster := createCluster()
  142. cluster.Hosts = clusterHosts[:1]
  143. cluster.DiscoverHosts = true
  144. session, err := cluster.CreateSession()
  145. if err != nil {
  146. t.Fatalf("got error connecting to the cluster %v", err)
  147. }
  148. if *clusterSize > 1 {
  149. // wait for autodiscovery to update the pool with the list of known hosts
  150. time.Sleep(*flagAutoWait)
  151. }
  152. size := len(session.Pool.(*SimplePool).connPool)
  153. if *clusterSize != size {
  154. t.Logf("WARN: Expected a cluster size of %d, but actual size was %d", *clusterSize, size)
  155. }
  156. session.Close()
  157. }
  158. func TestEmptyHosts(t *testing.T) {
  159. cluster := createCluster()
  160. cluster.Hosts = nil
  161. if session, err := cluster.CreateSession(); err == nil {
  162. session.Close()
  163. t.Error("expected err, got nil")
  164. }
  165. }
  166. //TestUseStatementError checks to make sure the correct error is returned when the user tries to execute a use statement.
  167. func TestUseStatementError(t *testing.T) {
  168. session := createSession(t)
  169. defer session.Close()
  170. if err := session.Query("USE gocql_test").Exec(); err != nil {
  171. if err != ErrUseStmt {
  172. t.Error("expected ErrUseStmt, got " + err.Error())
  173. }
  174. } else {
  175. t.Error("expected err, got nil.")
  176. }
  177. }
  178. //TestInvalidKeyspace checks that an invalid keyspace will return promptly and without a flood of connections
  179. func TestInvalidKeyspace(t *testing.T) {
  180. cluster := createCluster()
  181. cluster.Keyspace = "invalidKeyspace"
  182. session, err := cluster.CreateSession()
  183. if err != nil {
  184. if err != ErrNoConnectionsStarted {
  185. t.Errorf("Expected ErrNoConnections but got %v", err)
  186. }
  187. } else {
  188. session.Close() //Clean up the session
  189. t.Error("expected err, got nil.")
  190. }
  191. }
  192. func TestTracing(t *testing.T) {
  193. session := createSession(t)
  194. defer session.Close()
  195. if err := createTable(session, `CREATE TABLE trace (id int primary key)`); err != nil {
  196. t.Fatal("create:", err)
  197. }
  198. buf := &bytes.Buffer{}
  199. trace := NewTraceWriter(session, buf)
  200. if err := session.Query(`INSERT INTO trace (id) VALUES (?)`, 42).Trace(trace).Exec(); err != nil {
  201. t.Error("insert:", err)
  202. } else if buf.Len() == 0 {
  203. t.Error("insert: failed to obtain any tracing")
  204. }
  205. buf.Reset()
  206. var value int
  207. if err := session.Query(`SELECT id FROM trace WHERE id = ?`, 42).Trace(trace).Scan(&value); err != nil {
  208. t.Error("select:", err)
  209. } else if value != 42 {
  210. t.Errorf("value: expected %d, got %d", 42, value)
  211. } else if buf.Len() == 0 {
  212. t.Error("select: failed to obtain any tracing")
  213. }
  214. }
  215. func TestPaging(t *testing.T) {
  216. if *flagProto == 1 {
  217. t.Skip("Paging not supported. Please use Cassandra >= 2.0")
  218. }
  219. session := createSession(t)
  220. defer session.Close()
  221. if err := createTable(session, "CREATE TABLE paging (id int primary key)"); err != nil {
  222. t.Fatal("create table:", err)
  223. }
  224. for i := 0; i < 100; i++ {
  225. if err := session.Query("INSERT INTO paging (id) VALUES (?)", i).Exec(); err != nil {
  226. t.Fatal("insert:", err)
  227. }
  228. }
  229. iter := session.Query("SELECT id FROM paging").PageSize(10).Iter()
  230. var id int
  231. count := 0
  232. for iter.Scan(&id) {
  233. count++
  234. }
  235. if err := iter.Close(); err != nil {
  236. t.Fatal("close:", err)
  237. }
  238. if count != 100 {
  239. t.Fatalf("expected %d, got %d", 100, count)
  240. }
  241. }
  242. func TestCAS(t *testing.T) {
  243. if *flagProto == 1 {
  244. t.Skip("lightweight transactions not supported. Please use Cassandra >= 2.0")
  245. }
  246. session := createSession(t)
  247. defer session.Close()
  248. session.cfg.SerialConsistency = LocalSerial
  249. if err := createTable(session, `CREATE TABLE cas_table (
  250. title varchar,
  251. revid timeuuid,
  252. last_modified timestamp,
  253. PRIMARY KEY (title, revid)
  254. )`); err != nil {
  255. t.Fatal("create:", err)
  256. }
  257. title, revid, modified := "baz", TimeUUID(), time.Now()
  258. var titleCAS string
  259. var revidCAS UUID
  260. var modifiedCAS time.Time
  261. if applied, err := session.Query(`INSERT INTO cas_table (title, revid, last_modified)
  262. VALUES (?, ?, ?) IF NOT EXISTS`,
  263. title, revid, modified).ScanCAS(&titleCAS, &revidCAS, &modifiedCAS); err != nil {
  264. t.Fatal("insert:", err)
  265. } else if !applied {
  266. t.Fatal("insert should have been applied")
  267. }
  268. if applied, err := session.Query(`INSERT INTO cas_table (title, revid, last_modified)
  269. VALUES (?, ?, ?) IF NOT EXISTS`,
  270. title, revid, modified).ScanCAS(&titleCAS, &revidCAS, &modifiedCAS); err != nil {
  271. t.Fatal("insert:", err)
  272. } else if applied {
  273. t.Fatal("insert should not have been applied")
  274. } else if title != titleCAS || revid != revidCAS {
  275. t.Fatalf("expected %s/%v/%v but got %s/%v/%v", title, revid, modified, titleCAS, revidCAS, modifiedCAS)
  276. }
  277. tenSecondsLater := modified.Add(10 * time.Second)
  278. if applied, err := session.Query(`DELETE FROM cas_table WHERE title = ? and revid = ? IF last_modified = ?`,
  279. title, revid, tenSecondsLater).ScanCAS(&modifiedCAS); err != nil {
  280. t.Fatal("delete:", err)
  281. } else if applied {
  282. t.Fatal("delete should have not been applied")
  283. }
  284. if modifiedCAS.Unix() != tenSecondsLater.Add(-10*time.Second).Unix() {
  285. t.Fatalf("Was expecting modified CAS to be %v; but was one second later", modifiedCAS.UTC())
  286. }
  287. if _, err := session.Query(`DELETE FROM cas_table WHERE title = ? and revid = ? IF last_modified = ?`,
  288. title, revid, tenSecondsLater).ScanCAS(); err.Error() != "count mismatch" {
  289. t.Fatalf("delete: was expecting count mismatch error but got %s", err)
  290. }
  291. if applied, err := session.Query(`DELETE FROM cas_table WHERE title = ? and revid = ? IF last_modified = ?`,
  292. title, revid, modified).ScanCAS(&modifiedCAS); err != nil {
  293. t.Fatal("delete:", err)
  294. } else if !applied {
  295. t.Fatal("delete should have been applied")
  296. }
  297. }
  298. func TestMapScanCAS(t *testing.T) {
  299. if *flagProto == 1 {
  300. t.Skip("lightweight transactions not supported. Please use Cassandra >= 2.0")
  301. }
  302. session := createSession(t)
  303. defer session.Close()
  304. if err := createTable(session, `CREATE TABLE cas_table2 (
  305. title varchar,
  306. revid timeuuid,
  307. last_modified timestamp,
  308. deleted boolean,
  309. PRIMARY KEY (title, revid)
  310. )`); err != nil {
  311. t.Fatal("create:", err)
  312. }
  313. title, revid, modified, deleted := "baz", TimeUUID(), time.Now(), false
  314. mapCAS := map[string]interface{}{}
  315. if applied, err := session.Query(`INSERT INTO cas_table2 (title, revid, last_modified, deleted)
  316. VALUES (?, ?, ?, ?) IF NOT EXISTS`,
  317. title, revid, modified, deleted).MapScanCAS(mapCAS); err != nil {
  318. t.Fatal("insert:", err)
  319. } else if !applied {
  320. t.Fatal("insert should have been applied")
  321. }
  322. mapCAS = map[string]interface{}{}
  323. if applied, err := session.Query(`INSERT INTO cas_table2 (title, revid, last_modified, deleted)
  324. VALUES (?, ?, ?, ?) IF NOT EXISTS`,
  325. title, revid, modified, deleted).MapScanCAS(mapCAS); err != nil {
  326. t.Fatal("insert:", err)
  327. } else if applied {
  328. t.Fatal("insert should not have been applied")
  329. } else if title != mapCAS["title"] || revid != mapCAS["revid"] || deleted != mapCAS["deleted"] {
  330. t.Fatalf("expected %s/%v/%v/%v but got %s/%v/%v%v", title, revid, modified, false, mapCAS["title"], mapCAS["revid"], mapCAS["last_modified"], mapCAS["deleted"])
  331. }
  332. }
  333. func TestBatch(t *testing.T) {
  334. if *flagProto == 1 {
  335. t.Skip("atomic batches not supported. Please use Cassandra >= 2.0")
  336. }
  337. session := createSession(t)
  338. defer session.Close()
  339. if err := createTable(session, `CREATE TABLE batch_table (id int primary key)`); err != nil {
  340. t.Fatal("create table:", err)
  341. }
  342. batch := NewBatch(LoggedBatch)
  343. for i := 0; i < 100; i++ {
  344. batch.Query(`INSERT INTO batch_table (id) VALUES (?)`, i)
  345. }
  346. if err := session.ExecuteBatch(batch); err != nil {
  347. t.Fatal("execute batch:", err)
  348. }
  349. count := 0
  350. if err := session.Query(`SELECT COUNT(*) FROM batch_table`).Scan(&count); err != nil {
  351. t.Fatal("select count:", err)
  352. } else if count != 100 {
  353. t.Fatalf("count: expected %d, got %d\n", 100, count)
  354. }
  355. }
  356. func TestUnpreparedBatch(t *testing.T) {
  357. if *flagProto == 1 {
  358. t.Skip("atomic batches not supported. Please use Cassandra >= 2.0")
  359. }
  360. session := createSession(t)
  361. defer session.Close()
  362. if err := createTable(session, `CREATE TABLE batch_unprepared (id int primary key, c counter)`); err != nil {
  363. t.Fatal("create table:", err)
  364. }
  365. var batch *Batch
  366. if *flagProto == 2 {
  367. batch = NewBatch(CounterBatch)
  368. } else {
  369. batch = NewBatch(UnloggedBatch)
  370. }
  371. for i := 0; i < 100; i++ {
  372. batch.Query(`UPDATE batch_unprepared SET c = c + 1 WHERE id = 1`)
  373. }
  374. if err := session.ExecuteBatch(batch); err != nil {
  375. t.Fatal("execute batch:", err)
  376. }
  377. count := 0
  378. if err := session.Query(`SELECT COUNT(*) FROM batch_unprepared`).Scan(&count); err != nil {
  379. t.Fatal("select count:", err)
  380. } else if count != 1 {
  381. t.Fatalf("count: expected %d, got %d\n", 100, count)
  382. }
  383. if err := session.Query(`SELECT c FROM batch_unprepared`).Scan(&count); err != nil {
  384. t.Fatal("select count:", err)
  385. } else if count != 100 {
  386. t.Fatalf("count: expected %d, got %d\n", 100, count)
  387. }
  388. }
  389. // TestBatchLimit tests gocql to make sure batch operations larger than the maximum
  390. // statement limit are not submitted to a cassandra node.
  391. func TestBatchLimit(t *testing.T) {
  392. if *flagProto == 1 {
  393. t.Skip("atomic batches not supported. Please use Cassandra >= 2.0")
  394. }
  395. session := createSession(t)
  396. defer session.Close()
  397. if err := createTable(session, `CREATE TABLE batch_table2 (id int primary key)`); err != nil {
  398. t.Fatal("create table:", err)
  399. }
  400. batch := NewBatch(LoggedBatch)
  401. for i := 0; i < 65537; i++ {
  402. batch.Query(`INSERT INTO batch_table2 (id) VALUES (?)`, i)
  403. }
  404. if err := session.ExecuteBatch(batch); err != ErrTooManyStmts {
  405. t.Fatal("gocql attempted to execute a batch larger than the support limit of statements.")
  406. }
  407. }
  408. func TestWhereIn(t *testing.T) {
  409. session := createSession(t)
  410. defer session.Close()
  411. if err := createTable(session, `CREATE TABLE where_in_table (id int, cluster int, primary key (id,cluster))`); err != nil {
  412. t.Fatal("create table:", err)
  413. }
  414. if err := session.Query("INSERT INTO where_in_table (id, cluster) VALUES (?,?)", 100, 200).Exec(); err != nil {
  415. t.Fatal("insert:", err)
  416. }
  417. iter := session.Query("SELECT * FROM where_in_table WHERE id = ? AND cluster IN (?)", 100, 200).Iter()
  418. var id, cluster int
  419. count := 0
  420. for iter.Scan(&id, &cluster) {
  421. count++
  422. }
  423. if id != 100 || cluster != 200 {
  424. t.Fatalf("Was expecting id and cluster to be (100,200) but were (%d,%d)", id, cluster)
  425. }
  426. }
  427. // TestTooManyQueryArgs tests to make sure the library correctly handles the application level bug
  428. // whereby too many query arguments are passed to a query
  429. func TestTooManyQueryArgs(t *testing.T) {
  430. if *flagProto == 1 {
  431. t.Skip("atomic batches not supported. Please use Cassandra >= 2.0")
  432. }
  433. session := createSession(t)
  434. defer session.Close()
  435. if err := createTable(session, `CREATE TABLE too_many_query_args (id int primary key, value int)`); err != nil {
  436. t.Fatal("create table:", err)
  437. }
  438. _, err := session.Query(`SELECT * FROM too_many_query_args WHERE id = ?`, 1, 2).Iter().SliceMap()
  439. if err == nil {
  440. t.Fatal("'`SELECT * FROM too_many_query_args WHERE id = ?`, 1, 2' should return an ErrQueryArgLength")
  441. }
  442. if err != ErrQueryArgLength {
  443. t.Fatalf("'`SELECT * FROM too_many_query_args WHERE id = ?`, 1, 2' should return an ErrQueryArgLength, but returned: %s", err)
  444. }
  445. batch := session.NewBatch(UnloggedBatch)
  446. batch.Query("INSERT INTO too_many_query_args (id, value) VALUES (?, ?)", 1, 2, 3)
  447. err = session.ExecuteBatch(batch)
  448. if err == nil {
  449. t.Fatal("'`INSERT INTO too_many_query_args (id, value) VALUES (?, ?)`, 1, 2, 3' should return an ErrQueryArgLength")
  450. }
  451. if err != ErrQueryArgLength {
  452. t.Fatalf("'INSERT INTO too_many_query_args (id, value) VALUES (?, ?)`, 1, 2, 3' should return an ErrQueryArgLength, but returned: %s", err)
  453. }
  454. }
  455. // TestNotEnoughQueryArgs tests to make sure the library correctly handles the application level bug
  456. // whereby not enough query arguments are passed to a query
  457. func TestNotEnoughQueryArgs(t *testing.T) {
  458. if *flagProto == 1 {
  459. t.Skip("atomic batches not supported. Please use Cassandra >= 2.0")
  460. }
  461. session := createSession(t)
  462. defer session.Close()
  463. if err := createTable(session, `CREATE TABLE not_enough_query_args (id int, cluster int, value int, primary key (id, cluster))`); err != nil {
  464. t.Fatal("create table:", err)
  465. }
  466. _, err := session.Query(`SELECT * FROM not_enough_query_args WHERE id = ? and cluster = ?`, 1).Iter().SliceMap()
  467. if err == nil {
  468. t.Fatal("'`SELECT * FROM not_enough_query_args WHERE id = ? and cluster = ?`, 1' should return an ErrQueryArgLength")
  469. }
  470. if err != ErrQueryArgLength {
  471. t.Fatalf("'`SELECT * FROM too_few_query_args WHERE id = ? and cluster = ?`, 1' should return an ErrQueryArgLength, but returned: %s", err)
  472. }
  473. batch := session.NewBatch(UnloggedBatch)
  474. batch.Query("INSERT INTO not_enough_query_args (id, cluster, value) VALUES (?, ?, ?)", 1, 2)
  475. err = session.ExecuteBatch(batch)
  476. if err == nil {
  477. t.Fatal("'`INSERT INTO not_enough_query_args (id, cluster, value) VALUES (?, ?, ?)`, 1, 2' should return an ErrQueryArgLength")
  478. }
  479. if err != ErrQueryArgLength {
  480. t.Fatalf("'INSERT INTO not_enough_query_args (id, cluster, value) VALUES (?, ?, ?)`, 1, 2' should return an ErrQueryArgLength, but returned: %s", err)
  481. }
  482. }
  483. // TestCreateSessionTimeout tests to make sure the CreateSession function timeouts out correctly
  484. // and prevents an infinite loop of connection retries.
  485. func TestCreateSessionTimeout(t *testing.T) {
  486. go func() {
  487. <-time.After(2 * time.Second)
  488. t.Error("no startup timeout")
  489. }()
  490. cluster := createCluster()
  491. cluster.Hosts = []string{"127.0.0.1:1"}
  492. session, err := cluster.CreateSession()
  493. if err == nil {
  494. session.Close()
  495. t.Fatal("expected ErrNoConnectionsStarted, but no error was returned.")
  496. }
  497. if err != ErrNoConnectionsStarted {
  498. t.Fatalf("expected ErrNoConnectionsStarted, but received %v", err)
  499. }
  500. }
  501. type FullName struct {
  502. FirstName string
  503. LastName string
  504. }
  505. func (n FullName) MarshalCQL(info TypeInfo) ([]byte, error) {
  506. return []byte(n.FirstName + " " + n.LastName), nil
  507. }
  508. func (n *FullName) UnmarshalCQL(info TypeInfo, data []byte) error {
  509. t := strings.SplitN(string(data), " ", 2)
  510. n.FirstName, n.LastName = t[0], t[1]
  511. return nil
  512. }
  513. func TestMapScanWithRefMap(t *testing.T) {
  514. session := createSession(t)
  515. defer session.Close()
  516. if err := createTable(session, `CREATE TABLE scan_map_ref_table (
  517. testtext text PRIMARY KEY,
  518. testfullname text,
  519. testint int,
  520. )`); err != nil {
  521. t.Fatal("create table:", err)
  522. }
  523. m := make(map[string]interface{})
  524. m["testtext"] = "testtext"
  525. m["testfullname"] = FullName{"John", "Doe"}
  526. m["testint"] = 100
  527. if err := session.Query(`INSERT INTO scan_map_ref_table (testtext, testfullname, testint) values (?,?,?)`, m["testtext"], m["testfullname"], m["testint"]).Exec(); err != nil {
  528. t.Fatal("insert:", err)
  529. }
  530. var testText string
  531. var testFullName FullName
  532. ret := map[string]interface{}{
  533. "testtext": &testText,
  534. "testfullname": &testFullName,
  535. // testint is not set here.
  536. }
  537. iter := session.Query(`SELECT * FROM scan_map_ref_table`).Iter()
  538. if ok := iter.MapScan(ret); !ok {
  539. t.Fatal("select:", iter.Close())
  540. } else {
  541. if ret["testtext"] != "testtext" {
  542. t.Fatal("returned testtext did not match")
  543. }
  544. f := ret["testfullname"].(FullName)
  545. if f.FirstName != "John" || f.LastName != "Doe" {
  546. t.Fatal("returned testfullname did not match")
  547. }
  548. if ret["testint"] != 100 {
  549. t.Fatal("returned testinit did not match")
  550. }
  551. }
  552. }
  553. func TestSliceMap(t *testing.T) {
  554. session := createSession(t)
  555. defer session.Close()
  556. if err := createTable(session, `CREATE TABLE slice_map_table (
  557. testuuid timeuuid PRIMARY KEY,
  558. testtimestamp timestamp,
  559. testvarchar varchar,
  560. testbigint bigint,
  561. testblob blob,
  562. testbool boolean,
  563. testfloat float,
  564. testdouble double,
  565. testint int,
  566. testdecimal decimal,
  567. testlist list<text>,
  568. testset set<int>,
  569. testmap map<varchar, varchar>,
  570. testvarint varint,
  571. testinet inet
  572. )`); err != nil {
  573. t.Fatal("create table:", err)
  574. }
  575. m := make(map[string]interface{})
  576. bigInt := new(big.Int)
  577. if _, ok := bigInt.SetString("830169365738487321165427203929228", 10); !ok {
  578. t.Fatal("Failed setting bigint by string")
  579. }
  580. m["testuuid"] = TimeUUID()
  581. m["testvarchar"] = "Test VarChar"
  582. m["testbigint"] = time.Now().Unix()
  583. m["testtimestamp"] = time.Now().Truncate(time.Millisecond).UTC()
  584. m["testblob"] = []byte("test blob")
  585. m["testbool"] = true
  586. m["testfloat"] = float32(4.564)
  587. m["testdouble"] = float64(4.815162342)
  588. m["testint"] = 2343
  589. m["testdecimal"] = inf.NewDec(100, 0)
  590. m["testlist"] = []string{"quux", "foo", "bar", "baz", "quux"}
  591. m["testset"] = []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
  592. m["testmap"] = map[string]string{"field1": "val1", "field2": "val2", "field3": "val3"}
  593. m["testvarint"] = bigInt
  594. m["testinet"] = "213.212.2.19"
  595. sliceMap := []map[string]interface{}{m}
  596. if err := session.Query(`INSERT INTO slice_map_table (testuuid, testtimestamp, testvarchar, testbigint, testblob, testbool, testfloat, testdouble, testint, testdecimal, testlist, testset, testmap, testvarint, testinet) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
  597. m["testuuid"], m["testtimestamp"], m["testvarchar"], m["testbigint"], m["testblob"], m["testbool"], m["testfloat"], m["testdouble"], m["testint"], m["testdecimal"], m["testlist"], m["testset"], m["testmap"], m["testvarint"], m["testinet"]).Exec(); err != nil {
  598. t.Fatal("insert:", err)
  599. }
  600. if returned, retErr := session.Query(`SELECT * FROM slice_map_table`).Iter().SliceMap(); retErr != nil {
  601. t.Fatal("select:", retErr)
  602. } else {
  603. matchSliceMap(t, sliceMap, returned[0])
  604. }
  605. // Test for Iter.MapScan()
  606. {
  607. testMap := make(map[string]interface{})
  608. if !session.Query(`SELECT * FROM slice_map_table`).Iter().MapScan(testMap) {
  609. t.Fatal("MapScan failed to work with one row")
  610. }
  611. matchSliceMap(t, sliceMap, testMap)
  612. }
  613. // Test for Query.MapScan()
  614. {
  615. testMap := make(map[string]interface{})
  616. if session.Query(`SELECT * FROM slice_map_table`).MapScan(testMap) != nil {
  617. t.Fatal("MapScan failed to work with one row")
  618. }
  619. matchSliceMap(t, sliceMap, testMap)
  620. }
  621. }
  622. func matchSliceMap(t *testing.T, sliceMap []map[string]interface{}, testMap map[string]interface{}) {
  623. if sliceMap[0]["testuuid"] != testMap["testuuid"] {
  624. t.Fatal("returned testuuid did not match")
  625. }
  626. if sliceMap[0]["testtimestamp"] != testMap["testtimestamp"] {
  627. t.Fatal("returned testtimestamp did not match")
  628. }
  629. if sliceMap[0]["testvarchar"] != testMap["testvarchar"] {
  630. t.Fatal("returned testvarchar did not match")
  631. }
  632. if sliceMap[0]["testbigint"] != testMap["testbigint"] {
  633. t.Fatal("returned testbigint did not match")
  634. }
  635. if !reflect.DeepEqual(sliceMap[0]["testblob"], testMap["testblob"]) {
  636. t.Fatal("returned testblob did not match")
  637. }
  638. if sliceMap[0]["testbool"] != testMap["testbool"] {
  639. t.Fatal("returned testbool did not match")
  640. }
  641. if sliceMap[0]["testfloat"] != testMap["testfloat"] {
  642. t.Fatal("returned testfloat did not match")
  643. }
  644. if sliceMap[0]["testdouble"] != testMap["testdouble"] {
  645. t.Fatal("returned testdouble did not match")
  646. }
  647. if sliceMap[0]["testinet"] != testMap["testinet"] {
  648. t.Fatal("returned testinet did not match")
  649. }
  650. expectedDecimal := sliceMap[0]["testdecimal"].(*inf.Dec)
  651. returnedDecimal := testMap["testdecimal"].(*inf.Dec)
  652. if expectedDecimal.Cmp(returnedDecimal) != 0 {
  653. t.Fatal("returned testdecimal did not match")
  654. }
  655. if !reflect.DeepEqual(sliceMap[0]["testlist"], testMap["testlist"]) {
  656. t.Fatal("returned testlist did not match")
  657. }
  658. if !reflect.DeepEqual(sliceMap[0]["testset"], testMap["testset"]) {
  659. t.Fatal("returned testset did not match")
  660. }
  661. if !reflect.DeepEqual(sliceMap[0]["testmap"], testMap["testmap"]) {
  662. t.Fatal("returned testmap did not match")
  663. }
  664. if sliceMap[0]["testint"] != testMap["testint"] {
  665. t.Fatal("returned testint did not match")
  666. }
  667. }
  668. func TestScanWithNilArguments(t *testing.T) {
  669. session := createSession(t)
  670. defer session.Close()
  671. if err := createTable(session, `CREATE TABLE scan_with_nil_arguments (
  672. foo varchar,
  673. bar int,
  674. PRIMARY KEY (foo, bar)
  675. )`); err != nil {
  676. t.Fatal("create:", err)
  677. }
  678. for i := 1; i <= 20; i++ {
  679. if err := session.Query("INSERT INTO scan_with_nil_arguments (foo, bar) VALUES (?, ?)",
  680. "squares", i*i).Exec(); err != nil {
  681. t.Fatal("insert:", err)
  682. }
  683. }
  684. iter := session.Query("SELECT * FROM scan_with_nil_arguments WHERE foo = ?", "squares").Iter()
  685. var n int
  686. count := 0
  687. for iter.Scan(nil, &n) {
  688. count += n
  689. }
  690. if err := iter.Close(); err != nil {
  691. t.Fatal("close:", err)
  692. }
  693. if count != 2870 {
  694. t.Fatalf("expected %d, got %d", 2870, count)
  695. }
  696. }
  697. func TestScanCASWithNilArguments(t *testing.T) {
  698. if *flagProto == 1 {
  699. t.Skip("lightweight transactions not supported. Please use Cassandra >= 2.0")
  700. }
  701. session := createSession(t)
  702. defer session.Close()
  703. if err := createTable(session, `CREATE TABLE scan_cas_with_nil_arguments (
  704. foo varchar,
  705. bar varchar,
  706. PRIMARY KEY (foo, bar)
  707. )`); err != nil {
  708. t.Fatal("create:", err)
  709. }
  710. foo := "baz"
  711. var cas string
  712. if applied, err := session.Query(`INSERT INTO scan_cas_with_nil_arguments (foo, bar)
  713. VALUES (?, ?) IF NOT EXISTS`,
  714. foo, foo).ScanCAS(nil, nil); err != nil {
  715. t.Fatal("insert:", err)
  716. } else if !applied {
  717. t.Fatal("insert should have been applied")
  718. }
  719. if applied, err := session.Query(`INSERT INTO scan_cas_with_nil_arguments (foo, bar)
  720. VALUES (?, ?) IF NOT EXISTS`,
  721. foo, foo).ScanCAS(&cas, nil); err != nil {
  722. t.Fatal("insert:", err)
  723. } else if applied {
  724. t.Fatal("insert should not have been applied")
  725. } else if foo != cas {
  726. t.Fatalf("expected %v but got %v", foo, cas)
  727. }
  728. if applied, err := session.Query(`INSERT INTO scan_cas_with_nil_arguments (foo, bar)
  729. VALUES (?, ?) IF NOT EXISTS`,
  730. foo, foo).ScanCAS(nil, &cas); err != nil {
  731. t.Fatal("insert:", err)
  732. } else if applied {
  733. t.Fatal("insert should not have been applied")
  734. } else if foo != cas {
  735. t.Fatalf("expected %v but got %v", foo, cas)
  736. }
  737. }
  738. func TestRebindQueryInfo(t *testing.T) {
  739. session := createSession(t)
  740. defer session.Close()
  741. if err := createTable(session, "CREATE TABLE rebind_query (id int, value text, PRIMARY KEY (id))"); err != nil {
  742. t.Fatalf("failed to create table with error '%v'", err)
  743. }
  744. if err := session.Query("INSERT INTO rebind_query (id, value) VALUES (?, ?)", 23, "quux").Exec(); err != nil {
  745. t.Fatalf("insert into rebind_query failed, err '%v'", err)
  746. }
  747. if err := session.Query("INSERT INTO rebind_query (id, value) VALUES (?, ?)", 24, "w00t").Exec(); err != nil {
  748. t.Fatalf("insert into rebind_query failed, err '%v'", err)
  749. }
  750. q := session.Query("SELECT value FROM rebind_query WHERE ID = ?")
  751. q.Bind(23)
  752. iter := q.Iter()
  753. var value string
  754. for iter.Scan(&value) {
  755. }
  756. if value != "quux" {
  757. t.Fatalf("expected %v but got %v", "quux", value)
  758. }
  759. q.Bind(24)
  760. iter = q.Iter()
  761. for iter.Scan(&value) {
  762. }
  763. if value != "w00t" {
  764. t.Fatalf("expected %v but got %v", "quux", value)
  765. }
  766. }
  767. //TestStaticQueryInfo makes sure that the application can manually bind query parameters using the simplest possible static binding strategy
  768. func TestStaticQueryInfo(t *testing.T) {
  769. session := createSession(t)
  770. defer session.Close()
  771. if err := createTable(session, "CREATE TABLE static_query_info (id int, value text, PRIMARY KEY (id))"); err != nil {
  772. t.Fatalf("failed to create table with error '%v'", err)
  773. }
  774. if err := session.Query("INSERT INTO static_query_info (id, value) VALUES (?, ?)", 113, "foo").Exec(); err != nil {
  775. t.Fatalf("insert into static_query_info failed, err '%v'", err)
  776. }
  777. autobinder := func(q *QueryInfo) ([]interface{}, error) {
  778. values := make([]interface{}, 1)
  779. values[0] = 113
  780. return values, nil
  781. }
  782. qry := session.Bind("SELECT id, value FROM static_query_info WHERE id = ?", autobinder)
  783. if err := qry.Exec(); err != nil {
  784. t.Fatalf("expose query info failed, error '%v'", err)
  785. }
  786. iter := qry.Iter()
  787. var id int
  788. var value string
  789. iter.Scan(&id, &value)
  790. if err := iter.Close(); err != nil {
  791. t.Fatalf("query with exposed info failed, err '%v'", err)
  792. }
  793. if value != "foo" {
  794. t.Fatalf("Expected value %s, but got %s", "foo", value)
  795. }
  796. }
  797. type ClusteredKeyValue struct {
  798. Id int
  799. Cluster int
  800. Value string
  801. }
  802. func (kv *ClusteredKeyValue) Bind(q *QueryInfo) ([]interface{}, error) {
  803. values := make([]interface{}, len(q.Args))
  804. for i, info := range q.Args {
  805. fieldName := upcaseInitial(info.Name)
  806. value := reflect.ValueOf(kv)
  807. field := reflect.Indirect(value).FieldByName(fieldName)
  808. values[i] = field.Addr().Interface()
  809. }
  810. return values, nil
  811. }
  812. func upcaseInitial(str string) string {
  813. for i, v := range str {
  814. return string(unicode.ToUpper(v)) + str[i+1:]
  815. }
  816. return ""
  817. }
  818. //TestBoundQueryInfo makes sure that the application can manually bind query parameters using the query meta data supplied at runtime
  819. func TestBoundQueryInfo(t *testing.T) {
  820. session := createSession(t)
  821. defer session.Close()
  822. if err := createTable(session, "CREATE TABLE clustered_query_info (id int, cluster int, value text, PRIMARY KEY (id, cluster))"); err != nil {
  823. t.Fatalf("failed to create table with error '%v'", err)
  824. }
  825. write := &ClusteredKeyValue{Id: 200, Cluster: 300, Value: "baz"}
  826. insert := session.Bind("INSERT INTO clustered_query_info (id, cluster, value) VALUES (?, ?,?)", write.Bind)
  827. if err := insert.Exec(); err != nil {
  828. t.Fatalf("insert into clustered_query_info failed, err '%v'", err)
  829. }
  830. read := &ClusteredKeyValue{Id: 200, Cluster: 300}
  831. qry := session.Bind("SELECT id, cluster, value FROM clustered_query_info WHERE id = ? and cluster = ?", read.Bind)
  832. iter := qry.Iter()
  833. var id, cluster int
  834. var value string
  835. iter.Scan(&id, &cluster, &value)
  836. if err := iter.Close(); err != nil {
  837. t.Fatalf("query with clustered_query_info info failed, err '%v'", err)
  838. }
  839. if value != "baz" {
  840. t.Fatalf("Expected value %s, but got %s", "baz", value)
  841. }
  842. }
  843. //TestBatchQueryInfo makes sure that the application can manually bind query parameters when executing in a batch
  844. func TestBatchQueryInfo(t *testing.T) {
  845. if *flagProto == 1 {
  846. t.Skip("atomic batches not supported. Please use Cassandra >= 2.0")
  847. }
  848. session := createSession(t)
  849. defer session.Close()
  850. if err := createTable(session, "CREATE TABLE batch_query_info (id int, cluster int, value text, PRIMARY KEY (id, cluster))"); err != nil {
  851. t.Fatalf("failed to create table with error '%v'", err)
  852. }
  853. write := func(q *QueryInfo) ([]interface{}, error) {
  854. values := make([]interface{}, 3)
  855. values[0] = 4000
  856. values[1] = 5000
  857. values[2] = "bar"
  858. return values, nil
  859. }
  860. batch := session.NewBatch(LoggedBatch)
  861. batch.Bind("INSERT INTO batch_query_info (id, cluster, value) VALUES (?, ?,?)", write)
  862. if err := session.ExecuteBatch(batch); err != nil {
  863. t.Fatalf("batch insert into batch_query_info failed, err '%v'", err)
  864. }
  865. read := func(q *QueryInfo) ([]interface{}, error) {
  866. values := make([]interface{}, 2)
  867. values[0] = 4000
  868. values[1] = 5000
  869. return values, nil
  870. }
  871. qry := session.Bind("SELECT id, cluster, value FROM batch_query_info WHERE id = ? and cluster = ?", read)
  872. iter := qry.Iter()
  873. var id, cluster int
  874. var value string
  875. iter.Scan(&id, &cluster, &value)
  876. if err := iter.Close(); err != nil {
  877. t.Fatalf("query with batch_query_info info failed, err '%v'", err)
  878. }
  879. if value != "bar" {
  880. t.Fatalf("Expected value %s, but got %s", "bar", value)
  881. }
  882. }
  883. func injectInvalidPreparedStatement(t *testing.T, session *Session, table string) (string, *Conn) {
  884. if err := createTable(session, `CREATE TABLE `+table+` (
  885. foo varchar,
  886. bar int,
  887. PRIMARY KEY (foo, bar)
  888. )`); err != nil {
  889. t.Fatal("create:", err)
  890. }
  891. stmt := "INSERT INTO " + table + " (foo, bar) VALUES (?, 7)"
  892. conn := session.Pool.Pick(nil)
  893. flight := new(inflightPrepare)
  894. stmtsLRU.Lock()
  895. stmtsLRU.lru.Add(conn.addr+stmt, flight)
  896. stmtsLRU.Unlock()
  897. flight.info = &resultPreparedFrame{
  898. preparedID: []byte{'f', 'o', 'o', 'b', 'a', 'r'},
  899. reqMeta: preparedMetadata{
  900. resultMetadata: resultMetadata{
  901. columns: []ColumnInfo{
  902. {
  903. Keyspace: "gocql_test",
  904. Table: table,
  905. Name: "foo",
  906. TypeInfo: NativeType{
  907. typ: TypeVarchar,
  908. },
  909. },
  910. },
  911. }},
  912. }
  913. return stmt, conn
  914. }
  915. func TestMissingSchemaPrepare(t *testing.T) {
  916. s := createSession(t)
  917. conn := s.Pool.Pick(nil)
  918. defer s.Close()
  919. insertQry := &Query{stmt: "INSERT INTO invalidschemaprep (val) VALUES (?)", values: []interface{}{5}, cons: s.cons,
  920. session: s, pageSize: s.pageSize, trace: s.trace,
  921. prefetch: s.prefetch, rt: s.cfg.RetryPolicy}
  922. if err := conn.executeQuery(insertQry).err; err == nil {
  923. t.Fatal("expected error, but got nil.")
  924. }
  925. if err := createTable(s, "CREATE TABLE invalidschemaprep (val int, PRIMARY KEY (val))"); err != nil {
  926. t.Fatal("create table:", err)
  927. }
  928. if err := conn.executeQuery(insertQry).err; err != nil {
  929. t.Fatal(err) // unconfigured columnfamily
  930. }
  931. }
  932. func TestReprepareStatement(t *testing.T) {
  933. session := createSession(t)
  934. defer session.Close()
  935. stmt, conn := injectInvalidPreparedStatement(t, session, "test_reprepare_statement")
  936. query := session.Query(stmt, "bar")
  937. if err := conn.executeQuery(query).Close(); err != nil {
  938. t.Fatalf("Failed to execute query for reprepare statement: %v", err)
  939. }
  940. }
  941. func TestReprepareBatch(t *testing.T) {
  942. if *flagProto == 1 {
  943. t.Skip("atomic batches not supported. Please use Cassandra >= 2.0")
  944. }
  945. session := createSession(t)
  946. defer session.Close()
  947. stmt, conn := injectInvalidPreparedStatement(t, session, "test_reprepare_statement_batch")
  948. batch := session.NewBatch(UnloggedBatch)
  949. batch.Query(stmt, "bar")
  950. if err := conn.executeBatch(batch); err != nil {
  951. t.Fatalf("Failed to execute query for reprepare statement: %v", err)
  952. }
  953. }
  954. func TestQueryInfo(t *testing.T) {
  955. session := createSession(t)
  956. defer session.Close()
  957. conn := session.Pool.Pick(nil)
  958. info, err := conn.prepareStatement("SELECT release_version, host_id FROM system.local WHERE key = ?", nil)
  959. if err != nil {
  960. t.Fatalf("Failed to execute query for preparing statement: %v", err)
  961. }
  962. if len(info.reqMeta.columns) != 1 {
  963. t.Fatalf("Was not expecting meta data for %d query arguments, but got %d\n", 1, len(info.reqMeta.columns))
  964. }
  965. if *flagProto > 1 {
  966. if len(info.respMeta.columns) != 2 {
  967. t.Fatalf("Was not expecting meta data for %d result columns, but got %d\n", 2, len(info.respMeta.columns))
  968. }
  969. }
  970. }
  971. //TestPreparedCacheEviction will make sure that the cache size is maintained
  972. func TestPreparedCacheEviction(t *testing.T) {
  973. session := createSession(t)
  974. defer session.Close()
  975. stmtsLRU.Lock()
  976. stmtsLRU.Max(4)
  977. stmtsLRU.Unlock()
  978. if err := createTable(session, "CREATE TABLE prepcachetest (id int,mod int,PRIMARY KEY (id))"); err != nil {
  979. t.Fatalf("failed to create table with error '%v'", err)
  980. }
  981. //Fill the table
  982. for i := 0; i < 2; i++ {
  983. if err := session.Query("INSERT INTO prepcachetest (id,mod) VALUES (?, ?)", i, 10000%(i+1)).Exec(); err != nil {
  984. t.Fatalf("insert into prepcachetest failed, err '%v'", err)
  985. }
  986. }
  987. //Populate the prepared statement cache with select statements
  988. var id, mod int
  989. for i := 0; i < 2; i++ {
  990. err := session.Query("SELECT id,mod FROM prepcachetest WHERE id = "+strconv.FormatInt(int64(i), 10)).Scan(&id, &mod)
  991. if err != nil {
  992. t.Fatalf("select from prepcachetest failed, error '%v'", err)
  993. }
  994. }
  995. //generate an update statement to test they are prepared
  996. err := session.Query("UPDATE prepcachetest SET mod = ? WHERE id = ?", 1, 11).Exec()
  997. if err != nil {
  998. t.Fatalf("update prepcachetest failed, error '%v'", err)
  999. }
  1000. //generate a delete statement to test they are prepared
  1001. err = session.Query("DELETE FROM prepcachetest WHERE id = ?", 1).Exec()
  1002. if err != nil {
  1003. t.Fatalf("delete from prepcachetest failed, error '%v'", err)
  1004. }
  1005. //generate an insert statement to test they are prepared
  1006. err = session.Query("INSERT INTO prepcachetest (id,mod) VALUES (?, ?)", 3, 11).Exec()
  1007. if err != nil {
  1008. t.Fatalf("insert into prepcachetest failed, error '%v'", err)
  1009. }
  1010. stmtsLRU.Lock()
  1011. //Make sure the cache size is maintained
  1012. if stmtsLRU.lru.Len() != stmtsLRU.lru.MaxEntries {
  1013. t.Fatalf("expected cache size of %v, got %v", stmtsLRU.lru.MaxEntries, stmtsLRU.lru.Len())
  1014. }
  1015. //Walk through all the configured hosts and test cache retention and eviction
  1016. var selFound, insFound, updFound, delFound, selEvict bool
  1017. for i := range session.cfg.Hosts {
  1018. _, ok := stmtsLRU.lru.Get(session.cfg.Hosts[i] + ":9042gocql_testSELECT id,mod FROM prepcachetest WHERE id = 1")
  1019. selFound = selFound || ok
  1020. _, ok = stmtsLRU.lru.Get(session.cfg.Hosts[i] + ":9042gocql_testINSERT INTO prepcachetest (id,mod) VALUES (?, ?)")
  1021. insFound = insFound || ok
  1022. _, ok = stmtsLRU.lru.Get(session.cfg.Hosts[i] + ":9042gocql_testUPDATE prepcachetest SET mod = ? WHERE id = ?")
  1023. updFound = updFound || ok
  1024. _, ok = stmtsLRU.lru.Get(session.cfg.Hosts[i] + ":9042gocql_testDELETE FROM prepcachetest WHERE id = ?")
  1025. delFound = delFound || ok
  1026. _, ok = stmtsLRU.lru.Get(session.cfg.Hosts[i] + ":9042gocql_testSELECT id,mod FROM prepcachetest WHERE id = 0")
  1027. selEvict = selEvict || !ok
  1028. }
  1029. stmtsLRU.Unlock()
  1030. if !selEvict {
  1031. t.Fatalf("expected first select statement to be purged, but statement was found in the cache.")
  1032. }
  1033. if !selFound {
  1034. t.Fatalf("expected second select statement to be cached, but statement was purged or not prepared.")
  1035. }
  1036. if !insFound {
  1037. t.Fatalf("expected insert statement to be cached, but statement was purged or not prepared.")
  1038. }
  1039. if !updFound {
  1040. t.Fatalf("expected update statement to be cached, but statement was purged or not prepared.")
  1041. }
  1042. if !delFound {
  1043. t.Error("expected delete statement to be cached, but statement was purged or not prepared.")
  1044. }
  1045. }
  1046. func TestPreparedCacheKey(t *testing.T) {
  1047. session := createSession(t)
  1048. defer session.Close()
  1049. // create a second keyspace
  1050. cluster2 := createCluster()
  1051. createKeyspace(t, cluster2, "gocql_test2")
  1052. cluster2.Keyspace = "gocql_test2"
  1053. session2, err := cluster2.CreateSession()
  1054. if err != nil {
  1055. t.Fatal("create session:", err)
  1056. }
  1057. defer session2.Close()
  1058. // both keyspaces have a table named "test_stmt_cache_key"
  1059. if err := createTable(session, "CREATE TABLE test_stmt_cache_key (id varchar primary key, field varchar)"); err != nil {
  1060. t.Fatal("create table:", err)
  1061. }
  1062. if err := createTable(session2, "CREATE TABLE test_stmt_cache_key (id varchar primary key, field varchar)"); err != nil {
  1063. t.Fatal("create table:", err)
  1064. }
  1065. // both tables have a single row with the same partition key but different column value
  1066. if err = session.Query(`INSERT INTO test_stmt_cache_key (id, field) VALUES (?, ?)`, "key", "one").Exec(); err != nil {
  1067. t.Fatal("insert:", err)
  1068. }
  1069. if err = session2.Query(`INSERT INTO test_stmt_cache_key (id, field) VALUES (?, ?)`, "key", "two").Exec(); err != nil {
  1070. t.Fatal("insert:", err)
  1071. }
  1072. // should be able to see different values in each keyspace
  1073. var value string
  1074. if err = session.Query("SELECT field FROM test_stmt_cache_key WHERE id = ?", "key").Scan(&value); err != nil {
  1075. t.Fatal("select:", err)
  1076. }
  1077. if value != "one" {
  1078. t.Errorf("Expected one, got %s", value)
  1079. }
  1080. if err = session2.Query("SELECT field FROM test_stmt_cache_key WHERE id = ?", "key").Scan(&value); err != nil {
  1081. t.Fatal("select:", err)
  1082. }
  1083. if value != "two" {
  1084. t.Errorf("Expected two, got %s", value)
  1085. }
  1086. }
  1087. //TestMarshalFloat64Ptr tests to see that a pointer to a float64 is marshalled correctly.
  1088. func TestMarshalFloat64Ptr(t *testing.T) {
  1089. session := createSession(t)
  1090. defer session.Close()
  1091. if err := createTable(session, "CREATE TABLE float_test (id double, test double, primary key (id))"); err != nil {
  1092. t.Fatal("create table:", err)
  1093. }
  1094. testNum := float64(7500)
  1095. if err := session.Query(`INSERT INTO float_test (id,test) VALUES (?,?)`, float64(7500.00), &testNum).Exec(); err != nil {
  1096. t.Fatal("insert float64:", err)
  1097. }
  1098. }
  1099. //TestMarshalInet tests to see that a pointer to a float64 is marshalled correctly.
  1100. func TestMarshalInet(t *testing.T) {
  1101. session := createSession(t)
  1102. defer session.Close()
  1103. if err := createTable(session, "CREATE TABLE inet_test (ip inet, name text, primary key (ip))"); err != nil {
  1104. t.Fatal("create table:", err)
  1105. }
  1106. stringIp := "123.34.45.56"
  1107. if err := session.Query(`INSERT INTO inet_test (ip,name) VALUES (?,?)`, stringIp, "Test IP 1").Exec(); err != nil {
  1108. t.Fatal("insert string inet:", err)
  1109. }
  1110. var stringResult string
  1111. if err := session.Query("SELECT ip FROM inet_test").Scan(&stringResult); err != nil {
  1112. t.Fatalf("select for string from inet_test 1 failed: %v", err)
  1113. }
  1114. if stringResult != stringIp {
  1115. t.Errorf("Expected %s, was %s", stringIp, stringResult)
  1116. }
  1117. var ipResult net.IP
  1118. if err := session.Query("SELECT ip FROM inet_test").Scan(&ipResult); err != nil {
  1119. t.Fatalf("select for net.IP from inet_test 1 failed: %v", err)
  1120. }
  1121. if ipResult.String() != stringIp {
  1122. t.Errorf("Expected %s, was %s", stringIp, ipResult.String())
  1123. }
  1124. if err := session.Query(`DELETE FROM inet_test WHERE ip = ?`, stringIp).Exec(); err != nil {
  1125. t.Fatal("delete inet table:", err)
  1126. }
  1127. netIp := net.ParseIP("222.43.54.65")
  1128. if err := session.Query(`INSERT INTO inet_test (ip,name) VALUES (?,?)`, netIp, "Test IP 2").Exec(); err != nil {
  1129. t.Fatal("insert netIp inet:", err)
  1130. }
  1131. if err := session.Query("SELECT ip FROM inet_test").Scan(&stringResult); err != nil {
  1132. t.Fatalf("select for string from inet_test 2 failed: %v", err)
  1133. }
  1134. if stringResult != netIp.String() {
  1135. t.Errorf("Expected %s, was %s", netIp.String(), stringResult)
  1136. }
  1137. if err := session.Query("SELECT ip FROM inet_test").Scan(&ipResult); err != nil {
  1138. t.Fatalf("select for net.IP from inet_test 2 failed: %v", err)
  1139. }
  1140. if ipResult.String() != netIp.String() {
  1141. t.Errorf("Expected %s, was %s", netIp.String(), ipResult.String())
  1142. }
  1143. }
  1144. func TestVarint(t *testing.T) {
  1145. session := createSession(t)
  1146. defer session.Close()
  1147. if err := createTable(session, "CREATE TABLE varint_test (id varchar, test varint, test2 varint, primary key (id))"); err != nil {
  1148. t.Fatalf("failed to create table with error '%v'", err)
  1149. }
  1150. if err := session.Query(`INSERT INTO varint_test (id, test) VALUES (?, ?)`, "id", 0).Exec(); err != nil {
  1151. t.Fatalf("insert varint: %v", err)
  1152. }
  1153. var result int
  1154. if err := session.Query("SELECT test FROM varint_test").Scan(&result); err != nil {
  1155. t.Fatalf("select from varint_test failed: %v", err)
  1156. }
  1157. if result != 0 {
  1158. t.Errorf("Expected 0, was %d", result)
  1159. }
  1160. if err := session.Query(`INSERT INTO varint_test (id, test) VALUES (?, ?)`, "id", -1).Exec(); err != nil {
  1161. t.Fatalf("insert varint: %v", err)
  1162. }
  1163. if err := session.Query("SELECT test FROM varint_test").Scan(&result); err != nil {
  1164. t.Fatalf("select from varint_test failed: %v", err)
  1165. }
  1166. if result != -1 {
  1167. t.Errorf("Expected -1, was %d", result)
  1168. }
  1169. if err := session.Query(`INSERT INTO varint_test (id, test) VALUES (?, ?)`, "id", int64(math.MaxInt32)+1).Exec(); err != nil {
  1170. t.Fatalf("insert varint: %v", err)
  1171. }
  1172. var result64 int64
  1173. if err := session.Query("SELECT test FROM varint_test").Scan(&result64); err != nil {
  1174. t.Fatalf("select from varint_test failed: %v", err)
  1175. }
  1176. if result64 != int64(math.MaxInt32)+1 {
  1177. t.Errorf("Expected %d, was %d", int64(math.MaxInt32)+1, result64)
  1178. }
  1179. biggie := new(big.Int)
  1180. biggie.SetString("36893488147419103232", 10) // > 2**64
  1181. if err := session.Query(`INSERT INTO varint_test (id, test) VALUES (?, ?)`, "id", biggie).Exec(); err != nil {
  1182. t.Fatalf("insert varint: %v", err)
  1183. }
  1184. resultBig := new(big.Int)
  1185. if err := session.Query("SELECT test FROM varint_test").Scan(resultBig); err != nil {
  1186. t.Fatalf("select from varint_test failed: %v", err)
  1187. }
  1188. if resultBig.String() != biggie.String() {
  1189. t.Errorf("Expected %s, was %s", biggie.String(), resultBig.String())
  1190. }
  1191. err := session.Query("SELECT test FROM varint_test").Scan(&result64)
  1192. if err == nil || strings.Index(err.Error(), "out of range") == -1 {
  1193. t.Errorf("expected out of range error since value is too big for int64")
  1194. }
  1195. // value not set in cassandra, leave bind variable empty
  1196. resultBig = new(big.Int)
  1197. if err := session.Query("SELECT test2 FROM varint_test").Scan(resultBig); err != nil {
  1198. t.Fatalf("select from varint_test failed: %v", err)
  1199. }
  1200. if resultBig.Int64() != 0 {
  1201. t.Errorf("Expected %s, was %s", biggie.String(), resultBig.String())
  1202. }
  1203. // can use double pointer to explicitly detect value is not set in cassandra
  1204. if err := session.Query("SELECT test2 FROM varint_test").Scan(&resultBig); err != nil {
  1205. t.Fatalf("select from varint_test failed: %v", err)
  1206. }
  1207. if resultBig != nil {
  1208. t.Errorf("Expected %v, was %v", nil, *resultBig)
  1209. }
  1210. }
  1211. //TestQueryStats confirms that the stats are returning valid data. Accuracy may be questionable.
  1212. func TestQueryStats(t *testing.T) {
  1213. session := createSession(t)
  1214. defer session.Close()
  1215. qry := session.Query("SELECT * FROM system.peers")
  1216. if err := qry.Exec(); err != nil {
  1217. t.Fatalf("query failed. %v", err)
  1218. } else {
  1219. if qry.Attempts() < 1 {
  1220. t.Fatal("expected at least 1 attempt, but got 0")
  1221. }
  1222. if qry.Latency() <= 0 {
  1223. t.Fatalf("expected latency to be greater than 0, but got %v instead.", qry.Latency())
  1224. }
  1225. }
  1226. }
  1227. //TestBatchStats confirms that the stats are returning valid data. Accuracy may be questionable.
  1228. func TestBatchStats(t *testing.T) {
  1229. if *flagProto == 1 {
  1230. t.Skip("atomic batches not supported. Please use Cassandra >= 2.0")
  1231. }
  1232. session := createSession(t)
  1233. defer session.Close()
  1234. if err := createTable(session, "CREATE TABLE batchStats (id int, PRIMARY KEY (id))"); err != nil {
  1235. t.Fatalf("failed to create table with error '%v'", err)
  1236. }
  1237. b := session.NewBatch(LoggedBatch)
  1238. b.Query("INSERT INTO batchStats (id) VALUES (?)", 1)
  1239. b.Query("INSERT INTO batchStats (id) VALUES (?)", 2)
  1240. if err := session.ExecuteBatch(b); err != nil {
  1241. t.Fatalf("query failed. %v", err)
  1242. } else {
  1243. if b.Attempts() < 1 {
  1244. t.Fatal("expected at least 1 attempt, but got 0")
  1245. }
  1246. if b.Latency() <= 0 {
  1247. t.Fatalf("expected latency to be greater than 0, but got %v instead.", b.Latency())
  1248. }
  1249. }
  1250. }
  1251. //TestNilInQuery tests to see that a nil value passed to a query is handled by Cassandra
  1252. //TODO validate the nil value by reading back the nil. Need to fix Unmarshalling.
  1253. func TestNilInQuery(t *testing.T) {
  1254. session := createSession(t)
  1255. defer session.Close()
  1256. if err := createTable(session, "CREATE TABLE testNilInsert (id int, count int, PRIMARY KEY (id))"); err != nil {
  1257. t.Fatalf("failed to create table with error '%v'", err)
  1258. }
  1259. if err := session.Query("INSERT INTO testNilInsert (id,count) VALUES (?,?)", 1, nil).Exec(); err != nil {
  1260. t.Fatalf("failed to insert with err: %v", err)
  1261. }
  1262. var id int
  1263. if err := session.Query("SELECT id FROM testNilInsert").Scan(&id); err != nil {
  1264. t.Fatalf("failed to select with err: %v", err)
  1265. } else if id != 1 {
  1266. t.Fatalf("expected id to be 1, got %v", id)
  1267. }
  1268. }
  1269. // Don't initialize time.Time bind variable if cassandra timestamp column is empty
  1270. func TestEmptyTimestamp(t *testing.T) {
  1271. session := createSession(t)
  1272. defer session.Close()
  1273. if err := createTable(session, "CREATE TABLE test_empty_timestamp (id int, time timestamp, num int, PRIMARY KEY (id))"); err != nil {
  1274. t.Fatalf("failed to create table with error '%v'", err)
  1275. }
  1276. if err := session.Query("INSERT INTO test_empty_timestamp (id, num) VALUES (?,?)", 1, 561).Exec(); err != nil {
  1277. t.Fatalf("failed to insert with err: %v", err)
  1278. }
  1279. var timeVal time.Time
  1280. if err := session.Query("SELECT time FROM test_empty_timestamp where id = ?", 1).Scan(&timeVal); err != nil {
  1281. t.Fatalf("failed to select with err: %v", err)
  1282. }
  1283. if !timeVal.IsZero() {
  1284. t.Errorf("time.Time bind variable should still be empty (was %s)", timeVal)
  1285. }
  1286. }
  1287. // Integration test of just querying for data from the system.schema_keyspace table
  1288. func TestGetKeyspaceMetadata(t *testing.T) {
  1289. session := createSession(t)
  1290. defer session.Close()
  1291. keyspaceMetadata, err := getKeyspaceMetadata(session, "gocql_test")
  1292. if err != nil {
  1293. t.Fatalf("failed to query the keyspace metadata with err: %v", err)
  1294. }
  1295. if keyspaceMetadata == nil {
  1296. t.Fatal("failed to query the keyspace metadata, nil returned")
  1297. }
  1298. if keyspaceMetadata.Name != "gocql_test" {
  1299. t.Errorf("Expected keyspace name to be 'gocql' but was '%s'", keyspaceMetadata.Name)
  1300. }
  1301. if keyspaceMetadata.StrategyClass != "org.apache.cassandra.locator.SimpleStrategy" {
  1302. t.Errorf("Expected replication strategy class to be 'org.apache.cassandra.locator.SimpleStrategy' but was '%s'", keyspaceMetadata.StrategyClass)
  1303. }
  1304. if keyspaceMetadata.StrategyOptions == nil {
  1305. t.Error("Expected replication strategy options map but was nil")
  1306. }
  1307. rfStr, ok := keyspaceMetadata.StrategyOptions["replication_factor"]
  1308. if !ok {
  1309. t.Fatalf("Expected strategy option 'replication_factor' but was not found in %v", keyspaceMetadata.StrategyOptions)
  1310. }
  1311. rfInt, err := strconv.Atoi(rfStr.(string))
  1312. if err != nil {
  1313. t.Fatalf("Error converting string to int with err: %v", err)
  1314. }
  1315. if rfInt != *flagRF {
  1316. t.Errorf("Expected replication factor to be %d but was %d", *flagRF, rfInt)
  1317. }
  1318. }
  1319. // Integration test of just querying for data from the system.schema_columnfamilies table
  1320. func TestGetTableMetadata(t *testing.T) {
  1321. session := createSession(t)
  1322. defer session.Close()
  1323. if err := createTable(session, "CREATE TABLE test_table_metadata (first_id int, second_id int, third_id int, PRIMARY KEY (first_id, second_id))"); err != nil {
  1324. t.Fatalf("failed to create table with error '%v'", err)
  1325. }
  1326. tables, err := getTableMetadata(session, "gocql_test")
  1327. if err != nil {
  1328. t.Fatalf("failed to query the table metadata with err: %v", err)
  1329. }
  1330. if tables == nil {
  1331. t.Fatal("failed to query the table metadata, nil returned")
  1332. }
  1333. var testTable *TableMetadata
  1334. // verify all tables have minimum expected data
  1335. for i := range tables {
  1336. table := &tables[i]
  1337. if table.Name == "" {
  1338. t.Errorf("Expected table name to be set, but it was empty: index=%d metadata=%+v", i, table)
  1339. }
  1340. if table.Keyspace != "gocql_test" {
  1341. t.Errorf("Expected keyspace for '%d' table metadata to be 'gocql_test' but was '%s'", table.Name, table.Keyspace)
  1342. }
  1343. if table.KeyValidator == "" {
  1344. t.Errorf("Expected key validator to be set for table %s", table.Name)
  1345. }
  1346. if table.Comparator == "" {
  1347. t.Errorf("Expected comparator to be set for table %s", table.Name)
  1348. }
  1349. if table.DefaultValidator == "" {
  1350. t.Errorf("Expected default validator to be set for table %s", table.Name)
  1351. }
  1352. // these fields are not set until the metadata is compiled
  1353. if table.PartitionKey != nil {
  1354. t.Errorf("Did not expect partition key for table %s", table.Name)
  1355. }
  1356. if table.ClusteringColumns != nil {
  1357. t.Errorf("Did not expect clustering columns for table %s", table.Name)
  1358. }
  1359. if table.Columns != nil {
  1360. t.Errorf("Did not expect columns for table %s", table.Name)
  1361. }
  1362. // for the next part of the test after this loop, find the metadata for the test table
  1363. if table.Name == "test_table_metadata" {
  1364. testTable = table
  1365. }
  1366. }
  1367. // verify actual values on the test tables
  1368. if testTable == nil {
  1369. t.Fatal("Expected table metadata for name 'test_table_metadata'")
  1370. }
  1371. if testTable.KeyValidator != "org.apache.cassandra.db.marshal.Int32Type" {
  1372. t.Errorf("Expected test_table_metadata key validator to be 'org.apache.cassandra.db.marshal.Int32Type' but was '%s'", testTable.KeyValidator)
  1373. }
  1374. if testTable.Comparator != "org.apache.cassandra.db.marshal.CompositeType(org.apache.cassandra.db.marshal.Int32Type,org.apache.cassandra.db.marshal.UTF8Type)" {
  1375. t.Errorf("Expected test_table_metadata key validator to be 'org.apache.cassandra.db.marshal.CompositeType(org.apache.cassandra.db.marshal.Int32Type,org.apache.cassandra.db.marshal.UTF8Type)' but was '%s'", testTable.Comparator)
  1376. }
  1377. if testTable.DefaultValidator != "org.apache.cassandra.db.marshal.BytesType" {
  1378. t.Errorf("Expected test_table_metadata key validator to be 'org.apache.cassandra.db.marshal.BytesType' but was '%s'", testTable.DefaultValidator)
  1379. }
  1380. if *flagProto < protoVersion4 {
  1381. expectedKeyAliases := []string{"first_id"}
  1382. if !reflect.DeepEqual(testTable.KeyAliases, expectedKeyAliases) {
  1383. t.Errorf("Expected key aliases %v but was %v", expectedKeyAliases, testTable.KeyAliases)
  1384. }
  1385. expectedColumnAliases := []string{"second_id"}
  1386. if !reflect.DeepEqual(testTable.ColumnAliases, expectedColumnAliases) {
  1387. t.Errorf("Expected key aliases %v but was %v", expectedColumnAliases, testTable.ColumnAliases)
  1388. }
  1389. }
  1390. if testTable.ValueAlias != "" {
  1391. t.Errorf("Expected value alias '' but was '%s'", testTable.ValueAlias)
  1392. }
  1393. }
  1394. // Integration test of just querying for data from the system.schema_columns table
  1395. func TestGetColumnMetadata(t *testing.T) {
  1396. session := createSession(t)
  1397. defer session.Close()
  1398. if err := createTable(session, "CREATE TABLE test_column_metadata (first_id int, second_id int, third_id int, PRIMARY KEY (first_id, second_id))"); err != nil {
  1399. t.Fatalf("failed to create table with error '%v'", err)
  1400. }
  1401. if err := session.Query("CREATE INDEX index_column_metadata ON test_column_metadata ( third_id )").Exec(); err != nil {
  1402. t.Fatalf("failed to create index with err: %v", err)
  1403. }
  1404. columns, err := getColumnMetadata(session, "gocql_test")
  1405. if err != nil {
  1406. t.Fatalf("failed to query column metadata with err: %v", err)
  1407. }
  1408. if columns == nil {
  1409. t.Fatal("failed to query column metadata, nil returned")
  1410. }
  1411. testColumns := map[string]*ColumnMetadata{}
  1412. // verify actual values on the test columns
  1413. for i := range columns {
  1414. column := &columns[i]
  1415. if column.Name == "" {
  1416. t.Errorf("Expected column name to be set, but it was empty: index=%d metadata=%+v", i, column)
  1417. }
  1418. if column.Table == "" {
  1419. t.Errorf("Expected column %s table name to be set, but it was empty", column.Name)
  1420. }
  1421. if column.Keyspace != "gocql_test" {
  1422. t.Errorf("Expected column %s keyspace name to be 'gocql_test', but it was '%s'", column.Name, column.Keyspace)
  1423. }
  1424. if column.Kind == "" {
  1425. t.Errorf("Expected column %s kind to be set, but it was empty", column.Name)
  1426. }
  1427. if session.cfg.ProtoVersion == 1 && column.Kind != "regular" {
  1428. t.Errorf("Expected column %s kind to be set to 'regular' for proto V1 but it was '%s'", column.Name, column.Kind)
  1429. }
  1430. if column.Validator == "" {
  1431. t.Errorf("Expected column %s validator to be set, but it was empty", column.Name)
  1432. }
  1433. // find the test table columns for the next step after this loop
  1434. if column.Table == "test_column_metadata" {
  1435. testColumns[column.Name] = column
  1436. }
  1437. }
  1438. if *flagProto == 1 {
  1439. // V1 proto only returns "regular columns"
  1440. if len(testColumns) != 1 {
  1441. t.Errorf("Expected 1 test columns but there were %d", len(testColumns))
  1442. }
  1443. thirdID, found := testColumns["third_id"]
  1444. if !found {
  1445. t.Fatalf("Expected to find column 'third_id' metadata but there was only %v", testColumns)
  1446. }
  1447. if thirdID.Kind != REGULAR {
  1448. t.Errorf("Expected %s column kind to be '%s' but it was '%s'", thirdID.Name, REGULAR, thirdID.Kind)
  1449. }
  1450. if thirdID.Index.Name != "index_column_metadata" {
  1451. t.Errorf("Expected %s column index name to be 'index_column_metadata' but it was '%s'", thirdID.Name, thirdID.Index.Name)
  1452. }
  1453. } else {
  1454. if len(testColumns) != 3 {
  1455. t.Errorf("Expected 3 test columns but there were %d", len(testColumns))
  1456. }
  1457. firstID, found := testColumns["first_id"]
  1458. if !found {
  1459. t.Fatalf("Expected to find column 'first_id' metadata but there was only %v", testColumns)
  1460. }
  1461. secondID, found := testColumns["second_id"]
  1462. if !found {
  1463. t.Fatalf("Expected to find column 'second_id' metadata but there was only %v", testColumns)
  1464. }
  1465. thirdID, found := testColumns["third_id"]
  1466. if !found {
  1467. t.Fatalf("Expected to find column 'third_id' metadata but there was only %v", testColumns)
  1468. }
  1469. if firstID.Kind != PARTITION_KEY {
  1470. t.Errorf("Expected %s column kind to be '%s' but it was '%s'", firstID.Name, PARTITION_KEY, firstID.Kind)
  1471. }
  1472. if secondID.Kind != CLUSTERING_KEY {
  1473. t.Errorf("Expected %s column kind to be '%s' but it was '%s'", secondID.Name, CLUSTERING_KEY, secondID.Kind)
  1474. }
  1475. if thirdID.Kind != REGULAR {
  1476. t.Errorf("Expected %s column kind to be '%s' but it was '%s'", thirdID.Name, REGULAR, thirdID.Kind)
  1477. }
  1478. if thirdID.Index.Name != "index_column_metadata" {
  1479. t.Errorf("Expected %s column index name to be 'index_column_metadata' but it was '%s'", thirdID.Name, thirdID.Index.Name)
  1480. }
  1481. }
  1482. }
  1483. // Integration test of querying and composition the keyspace metadata
  1484. func TestKeyspaceMetadata(t *testing.T) {
  1485. session := createSession(t)
  1486. defer session.Close()
  1487. if err := createTable(session, "CREATE TABLE test_metadata (first_id int, second_id int, third_id int, PRIMARY KEY (first_id, second_id))"); err != nil {
  1488. t.Fatalf("failed to create table with error '%v'", err)
  1489. }
  1490. if err := session.Query("CREATE INDEX index_metadata ON test_metadata ( third_id )").Exec(); err != nil {
  1491. t.Fatalf("failed to create index with err: %v", err)
  1492. }
  1493. keyspaceMetadata, err := session.KeyspaceMetadata("gocql_test")
  1494. if err != nil {
  1495. t.Fatalf("failed to query keyspace metadata with err: %v", err)
  1496. }
  1497. if keyspaceMetadata == nil {
  1498. t.Fatal("expected the keyspace metadata to not be nil, but it was nil")
  1499. }
  1500. if keyspaceMetadata.Name != session.cfg.Keyspace {
  1501. t.Fatalf("Expected the keyspace name to be %s but was %s", session.cfg.Keyspace, keyspaceMetadata.Name)
  1502. }
  1503. if len(keyspaceMetadata.Tables) == 0 {
  1504. t.Errorf("Expected tables but there were none")
  1505. }
  1506. tableMetadata, found := keyspaceMetadata.Tables["test_metadata"]
  1507. if !found {
  1508. t.Fatalf("failed to find the test_metadata table metadata")
  1509. }
  1510. if len(tableMetadata.PartitionKey) != 1 {
  1511. t.Errorf("expected partition key length of 1, but was %d", len(tableMetadata.PartitionKey))
  1512. }
  1513. for i, column := range tableMetadata.PartitionKey {
  1514. if column == nil {
  1515. t.Errorf("partition key column metadata at index %d was nil", i)
  1516. }
  1517. }
  1518. if tableMetadata.PartitionKey[0].Name != "first_id" {
  1519. t.Errorf("Expected the first partition key column to be 'first_id' but was '%s'", tableMetadata.PartitionKey[0].Name)
  1520. }
  1521. if len(tableMetadata.ClusteringColumns) != 1 {
  1522. t.Fatalf("expected clustering columns length of 1, but was %d", len(tableMetadata.ClusteringColumns))
  1523. }
  1524. for i, column := range tableMetadata.ClusteringColumns {
  1525. if column == nil {
  1526. t.Fatalf("clustering column metadata at index %d was nil", i)
  1527. }
  1528. }
  1529. if tableMetadata.ClusteringColumns[0].Name != "second_id" {
  1530. t.Errorf("Expected the first clustering column to be 'second_id' but was '%s'", tableMetadata.ClusteringColumns[0].Name)
  1531. }
  1532. thirdColumn, found := tableMetadata.Columns["third_id"]
  1533. if !found {
  1534. t.Fatalf("Expected a column definition for 'third_id'")
  1535. }
  1536. if thirdColumn.Index.Name != "index_metadata" {
  1537. t.Errorf("Expected column index named 'index_metadata' but was '%s'", thirdColumn.Index.Name)
  1538. }
  1539. }
  1540. // Integration test of the routing key calculation
  1541. func TestRoutingKey(t *testing.T) {
  1542. session := createSession(t)
  1543. defer session.Close()
  1544. if err := createTable(session, "CREATE TABLE test_single_routing_key (first_id int, second_id int, PRIMARY KEY (first_id, second_id))"); err != nil {
  1545. t.Fatalf("failed to create table with error '%v'", err)
  1546. }
  1547. if err := createTable(session, "CREATE TABLE test_composite_routing_key (first_id int, second_id int, PRIMARY KEY ((first_id, second_id)))"); err != nil {
  1548. t.Fatalf("failed to create table with error '%v'", err)
  1549. }
  1550. routingKeyInfo, err := session.routingKeyInfo("SELECT * FROM test_single_routing_key WHERE second_id=? AND first_id=?")
  1551. if err != nil {
  1552. t.Fatalf("failed to get routing key info due to error: %v", err)
  1553. }
  1554. if routingKeyInfo == nil {
  1555. t.Fatal("Expected routing key info, but was nil")
  1556. }
  1557. if len(routingKeyInfo.indexes) != 1 {
  1558. t.Fatalf("Expected routing key indexes length to be 1 but was %d", len(routingKeyInfo.indexes))
  1559. }
  1560. if routingKeyInfo.indexes[0] != 1 {
  1561. t.Errorf("Expected routing key index[0] to be 1 but was %d", routingKeyInfo.indexes[0])
  1562. }
  1563. if len(routingKeyInfo.types) != 1 {
  1564. t.Fatalf("Expected routing key types length to be 1 but was %d", len(routingKeyInfo.types))
  1565. }
  1566. if routingKeyInfo.types[0] == nil {
  1567. t.Fatal("Expected routing key types[0] to be non-nil")
  1568. }
  1569. if routingKeyInfo.types[0].Type() != TypeInt {
  1570. t.Fatalf("Expected routing key types[0].Type to be %v but was %v", TypeInt, routingKeyInfo.types[0].Type())
  1571. }
  1572. // verify the cache is working
  1573. routingKeyInfo, err = session.routingKeyInfo("SELECT * FROM test_single_routing_key WHERE second_id=? AND first_id=?")
  1574. if err != nil {
  1575. t.Fatalf("failed to get routing key info due to error: %v", err)
  1576. }
  1577. if len(routingKeyInfo.indexes) != 1 {
  1578. t.Fatalf("Expected routing key indexes length to be 1 but was %d", len(routingKeyInfo.indexes))
  1579. }
  1580. if routingKeyInfo.indexes[0] != 1 {
  1581. t.Errorf("Expected routing key index[0] to be 1 but was %d", routingKeyInfo.indexes[0])
  1582. }
  1583. if len(routingKeyInfo.types) != 1 {
  1584. t.Fatalf("Expected routing key types length to be 1 but was %d", len(routingKeyInfo.types))
  1585. }
  1586. if routingKeyInfo.types[0] == nil {
  1587. t.Fatal("Expected routing key types[0] to be non-nil")
  1588. }
  1589. if routingKeyInfo.types[0].Type() != TypeInt {
  1590. t.Fatalf("Expected routing key types[0] to be %v but was %v", TypeInt, routingKeyInfo.types[0].Type())
  1591. }
  1592. cacheSize := session.routingKeyInfoCache.lru.Len()
  1593. if cacheSize != 1 {
  1594. t.Errorf("Expected cache size to be 1 but was %d", cacheSize)
  1595. }
  1596. query := session.Query("SELECT * FROM test_single_routing_key WHERE second_id=? AND first_id=?", 1, 2)
  1597. routingKey, err := query.GetRoutingKey()
  1598. if err != nil {
  1599. t.Fatalf("Failed to get routing key due to error: %v", err)
  1600. }
  1601. expectedRoutingKey := []byte{0, 0, 0, 2}
  1602. if !reflect.DeepEqual(expectedRoutingKey, routingKey) {
  1603. t.Errorf("Expected routing key %v but was %v", expectedRoutingKey, routingKey)
  1604. }
  1605. routingKeyInfo, err = session.routingKeyInfo("SELECT * FROM test_composite_routing_key WHERE second_id=? AND first_id=?")
  1606. if err != nil {
  1607. t.Fatalf("failed to get routing key info due to error: %v", err)
  1608. }
  1609. if routingKeyInfo == nil {
  1610. t.Fatal("Expected routing key info, but was nil")
  1611. }
  1612. if len(routingKeyInfo.indexes) != 2 {
  1613. t.Fatalf("Expected routing key indexes length to be 2 but was %d", len(routingKeyInfo.indexes))
  1614. }
  1615. if routingKeyInfo.indexes[0] != 1 {
  1616. t.Errorf("Expected routing key index[0] to be 1 but was %d", routingKeyInfo.indexes[0])
  1617. }
  1618. if routingKeyInfo.indexes[1] != 0 {
  1619. t.Errorf("Expected routing key index[1] to be 0 but was %d", routingKeyInfo.indexes[1])
  1620. }
  1621. if len(routingKeyInfo.types) != 2 {
  1622. t.Fatalf("Expected routing key types length to be 1 but was %d", len(routingKeyInfo.types))
  1623. }
  1624. if routingKeyInfo.types[0] == nil {
  1625. t.Fatal("Expected routing key types[0] to be non-nil")
  1626. }
  1627. if routingKeyInfo.types[0].Type() != TypeInt {
  1628. t.Fatalf("Expected routing key types[0] to be %v but was %v", TypeInt, routingKeyInfo.types[0].Type())
  1629. }
  1630. if routingKeyInfo.types[1] == nil {
  1631. t.Fatal("Expected routing key types[1] to be non-nil")
  1632. }
  1633. if routingKeyInfo.types[1].Type() != TypeInt {
  1634. t.Fatalf("Expected routing key types[0] to be %v but was %v", TypeInt, routingKeyInfo.types[1].Type())
  1635. }
  1636. query = session.Query("SELECT * FROM test_composite_routing_key WHERE second_id=? AND first_id=?", 1, 2)
  1637. routingKey, err = query.GetRoutingKey()
  1638. if err != nil {
  1639. t.Fatalf("Failed to get routing key due to error: %v", err)
  1640. }
  1641. expectedRoutingKey = []byte{0, 4, 0, 0, 0, 2, 0, 0, 4, 0, 0, 0, 1, 0}
  1642. if !reflect.DeepEqual(expectedRoutingKey, routingKey) {
  1643. t.Errorf("Expected routing key %v but was %v", expectedRoutingKey, routingKey)
  1644. }
  1645. // verify the cache is working
  1646. cacheSize = session.routingKeyInfoCache.lru.Len()
  1647. if cacheSize != 2 {
  1648. t.Errorf("Expected cache size to be 2 but was %d", cacheSize)
  1649. }
  1650. }
  1651. // Integration test of the token-aware policy-based connection pool
  1652. func TestTokenAwareConnPool(t *testing.T) {
  1653. cluster := createCluster()
  1654. cluster.ConnPoolType = NewTokenAwareConnPool
  1655. cluster.DiscoverHosts = true
  1656. // Drop and re-create the keyspace once. Different tests should use their own
  1657. // individual tables, but can assume that the table does not exist before.
  1658. initOnce.Do(func() {
  1659. createKeyspace(t, cluster, "gocql_test")
  1660. })
  1661. cluster.Keyspace = "gocql_test"
  1662. session, err := cluster.CreateSession()
  1663. if err != nil {
  1664. t.Fatal("createSession:", err)
  1665. }
  1666. defer session.Close()
  1667. if *clusterSize > 1 {
  1668. // wait for autodiscovery to update the pool with the list of known hosts
  1669. time.Sleep(*flagAutoWait)
  1670. }
  1671. if session.Pool.Size() != cluster.NumConns*len(cluster.Hosts) {
  1672. t.Errorf("Expected pool size %d but was %d", cluster.NumConns*len(cluster.Hosts), session.Pool.Size())
  1673. }
  1674. if err := createTable(session, "CREATE TABLE test_token_aware (id int, data text, PRIMARY KEY (id))"); err != nil {
  1675. t.Fatalf("failed to create test_token_aware table with err: %v", err)
  1676. }
  1677. query := session.Query("INSERT INTO test_token_aware (id, data) VALUES (?,?)", 42, "8 * 6 =")
  1678. if err := query.Exec(); err != nil {
  1679. t.Fatalf("failed to insert with err: %v", err)
  1680. }
  1681. query = session.Query("SELECT data FROM test_token_aware where id = ?", 42).Consistency(One)
  1682. iter := query.Iter()
  1683. var data string
  1684. if !iter.Scan(&data) {
  1685. t.Error("failed to scan data")
  1686. }
  1687. if err := iter.Close(); err != nil {
  1688. t.Errorf("iter failed with err: %v", err)
  1689. }
  1690. // TODO add verification that the query went to the correct host
  1691. }
  1692. type frameWriterFunc func(framer *framer, streamID int) error
  1693. func (f frameWriterFunc) writeFrame(framer *framer, streamID int) error {
  1694. return f(framer, streamID)
  1695. }
  1696. func TestStream0(t *testing.T) {
  1697. session := createSession(t)
  1698. defer session.Close()
  1699. var conn *Conn
  1700. for i := 0; i < 5; i++ {
  1701. if conn != nil {
  1702. break
  1703. }
  1704. conn = session.Pool.Pick(nil)
  1705. }
  1706. if conn == nil {
  1707. t.Fatal("no connections available in the pool")
  1708. }
  1709. writer := frameWriterFunc(func(f *framer, streamID int) error {
  1710. if streamID == 0 {
  1711. t.Fatal("should not use stream 0 for requests")
  1712. }
  1713. f.writeHeader(0, opError, streamID)
  1714. f.writeString("i am a bad frame")
  1715. f.wbuf[0] = 0xFF
  1716. return f.finishWrite()
  1717. })
  1718. const expErr = "gocql: error on stream 0:"
  1719. // need to write out an invalid frame, which we need a connection to do
  1720. frame, err := conn.exec(writer, nil)
  1721. if err == nil {
  1722. t.Fatal("expected to get an error on stream 0")
  1723. } else if !strings.HasPrefix(err.Error(), expErr) {
  1724. t.Fatalf("expected to get error prefix %q got %q", expErr, err.Error())
  1725. } else if frame != nil {
  1726. t.Fatalf("expected to get nil frame got %+v", frame)
  1727. }
  1728. }
  1729. func TestNegativeStream(t *testing.T) {
  1730. session := createSession(t)
  1731. defer session.Close()
  1732. var conn *Conn
  1733. for i := 0; i < 5; i++ {
  1734. if conn != nil {
  1735. break
  1736. }
  1737. conn = session.Pool.Pick(nil)
  1738. }
  1739. if conn == nil {
  1740. t.Fatal("no connections available in the pool")
  1741. }
  1742. const stream = -50
  1743. writer := frameWriterFunc(func(f *framer, streamID int) error {
  1744. f.writeHeader(0, opOptions, stream)
  1745. return f.finishWrite()
  1746. })
  1747. frame, err := conn.exec(writer, nil)
  1748. if err == nil {
  1749. t.Fatalf("expected to get an error on stream %d", stream)
  1750. } else if frame != nil {
  1751. t.Fatalf("expected to get nil frame got %+v", frame)
  1752. }
  1753. }