You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

1160 lines
36 KiB

  1. /*
  2. Copyright 2015 Google LLC
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package bigtable
  14. import (
  15. "fmt"
  16. "math/rand"
  17. "strings"
  18. "sync"
  19. "testing"
  20. "time"
  21. "cloud.google.com/go/internal/testutil"
  22. "golang.org/x/net/context"
  23. "google.golang.org/api/option"
  24. "google.golang.org/grpc"
  25. )
  26. func TestPrefix(t *testing.T) {
  27. tests := []struct {
  28. prefix, succ string
  29. }{
  30. {"", ""},
  31. {"\xff", ""}, // when used, "" means Infinity
  32. {"x\xff", "y"},
  33. {"\xfe", "\xff"},
  34. }
  35. for _, tc := range tests {
  36. got := prefixSuccessor(tc.prefix)
  37. if got != tc.succ {
  38. t.Errorf("prefixSuccessor(%q) = %q, want %s", tc.prefix, got, tc.succ)
  39. continue
  40. }
  41. r := PrefixRange(tc.prefix)
  42. if tc.succ == "" && r.limit != "" {
  43. t.Errorf("PrefixRange(%q) got limit %q", tc.prefix, r.limit)
  44. }
  45. if tc.succ != "" && r.limit != tc.succ {
  46. t.Errorf("PrefixRange(%q) got limit %q, want %q", tc.prefix, r.limit, tc.succ)
  47. }
  48. }
  49. }
  50. func TestApplyErrors(t *testing.T) {
  51. ctx := context.Background()
  52. table := &Table{
  53. c: &Client{
  54. project: "P",
  55. instance: "I",
  56. },
  57. table: "t",
  58. }
  59. f := ColumnFilter("C")
  60. m := NewMutation()
  61. m.DeleteRow()
  62. // Test nested conditional mutations.
  63. cm := NewCondMutation(f, NewCondMutation(f, m, nil), nil)
  64. if err := table.Apply(ctx, "x", cm); err == nil {
  65. t.Error("got nil, want error")
  66. }
  67. cm = NewCondMutation(f, nil, NewCondMutation(f, m, nil))
  68. if err := table.Apply(ctx, "x", cm); err == nil {
  69. t.Error("got nil, want error")
  70. }
  71. }
  72. func TestClientIntegration(t *testing.T) {
  73. start := time.Now()
  74. lastCheckpoint := start
  75. checkpoint := func(s string) {
  76. n := time.Now()
  77. t.Logf("[%s] %v since start, %v since last checkpoint", s, n.Sub(start), n.Sub(lastCheckpoint))
  78. lastCheckpoint = n
  79. }
  80. testEnv, err := NewIntegrationEnv()
  81. if err != nil {
  82. t.Fatalf("IntegrationEnv: %v", err)
  83. }
  84. var timeout time.Duration
  85. if testEnv.Config().UseProd {
  86. timeout = 10 * time.Minute
  87. t.Logf("Running test against production")
  88. } else {
  89. timeout = 1 * time.Minute
  90. t.Logf("bttest.Server running on %s", testEnv.Config().AdminEndpoint)
  91. }
  92. ctx, cancel := context.WithTimeout(context.Background(), timeout)
  93. defer cancel()
  94. client, err := testEnv.NewClient()
  95. if err != nil {
  96. t.Fatalf("Client: %v", err)
  97. }
  98. defer client.Close()
  99. checkpoint("dialed Client")
  100. adminClient, err := testEnv.NewAdminClient()
  101. if err != nil {
  102. t.Fatalf("AdminClient: %v", err)
  103. }
  104. defer adminClient.Close()
  105. checkpoint("dialed AdminClient")
  106. table := testEnv.Config().Table
  107. // Delete the table at the end of the test.
  108. // Do this even before creating the table so that if this is running
  109. // against production and CreateTable fails there's a chance of cleaning it up.
  110. defer adminClient.DeleteTable(ctx, table)
  111. if err := adminClient.CreateTable(ctx, table); err != nil {
  112. t.Fatalf("Creating table: %v", err)
  113. }
  114. checkpoint("created table")
  115. if err := adminClient.CreateColumnFamily(ctx, table, "follows"); err != nil {
  116. t.Fatalf("Creating column family: %v", err)
  117. }
  118. checkpoint(`created "follows" column family`)
  119. tbl := client.Open(table)
  120. // Insert some data.
  121. initialData := map[string][]string{
  122. "wmckinley": {"tjefferson"},
  123. "gwashington": {"jadams"},
  124. "tjefferson": {"gwashington", "jadams"}, // wmckinley set conditionally below
  125. "jadams": {"gwashington", "tjefferson"},
  126. }
  127. for row, ss := range initialData {
  128. mut := NewMutation()
  129. for _, name := range ss {
  130. mut.Set("follows", name, 1000, []byte("1"))
  131. }
  132. if err := tbl.Apply(ctx, row, mut); err != nil {
  133. t.Errorf("Mutating row %q: %v", row, err)
  134. }
  135. }
  136. checkpoint("inserted initial data")
  137. // TODO(igorbernstein): re-enable this when ready
  138. //if err := adminClient.WaitForReplication(ctx, table); err != nil {
  139. // t.Errorf("Waiting for replication for table %q: %v", table, err)
  140. //}
  141. //checkpoint("waited for replication")
  142. // Do a conditional mutation with a complex filter.
  143. mutTrue := NewMutation()
  144. mutTrue.Set("follows", "wmckinley", 1000, []byte("1"))
  145. filter := ChainFilters(ColumnFilter("gwash[iz].*"), ValueFilter("."))
  146. mut := NewCondMutation(filter, mutTrue, nil)
  147. if err := tbl.Apply(ctx, "tjefferson", mut); err != nil {
  148. t.Errorf("Conditionally mutating row: %v", err)
  149. }
  150. // Do a second condition mutation with a filter that does not match,
  151. // and thus no changes should be made.
  152. mutTrue = NewMutation()
  153. mutTrue.DeleteRow()
  154. filter = ColumnFilter("snoop.dogg")
  155. mut = NewCondMutation(filter, mutTrue, nil)
  156. if err := tbl.Apply(ctx, "tjefferson", mut); err != nil {
  157. t.Errorf("Conditionally mutating row: %v", err)
  158. }
  159. checkpoint("did two conditional mutations")
  160. // Fetch a row.
  161. row, err := tbl.ReadRow(ctx, "jadams")
  162. if err != nil {
  163. t.Fatalf("Reading a row: %v", err)
  164. }
  165. wantRow := Row{
  166. "follows": []ReadItem{
  167. {Row: "jadams", Column: "follows:gwashington", Timestamp: 1000, Value: []byte("1")},
  168. {Row: "jadams", Column: "follows:tjefferson", Timestamp: 1000, Value: []byte("1")},
  169. },
  170. }
  171. if !testutil.Equal(row, wantRow) {
  172. t.Errorf("Read row mismatch.\n got %#v\nwant %#v", row, wantRow)
  173. }
  174. checkpoint("tested ReadRow")
  175. // Do a bunch of reads with filters.
  176. readTests := []struct {
  177. desc string
  178. rr RowSet
  179. filter Filter // may be nil
  180. limit ReadOption // may be nil
  181. // We do the read, grab all the cells, turn them into "<row>-<col>-<val>",
  182. // and join with a comma.
  183. want string
  184. }{
  185. {
  186. desc: "read all, unfiltered",
  187. rr: RowRange{},
  188. want: "gwashington-jadams-1,jadams-gwashington-1,jadams-tjefferson-1,tjefferson-gwashington-1,tjefferson-jadams-1,tjefferson-wmckinley-1,wmckinley-tjefferson-1",
  189. },
  190. {
  191. desc: "read with InfiniteRange, unfiltered",
  192. rr: InfiniteRange("tjefferson"),
  193. want: "tjefferson-gwashington-1,tjefferson-jadams-1,tjefferson-wmckinley-1,wmckinley-tjefferson-1",
  194. },
  195. {
  196. desc: "read with NewRange, unfiltered",
  197. rr: NewRange("gargamel", "hubbard"),
  198. want: "gwashington-jadams-1",
  199. },
  200. {
  201. desc: "read with PrefixRange, unfiltered",
  202. rr: PrefixRange("jad"),
  203. want: "jadams-gwashington-1,jadams-tjefferson-1",
  204. },
  205. {
  206. desc: "read with SingleRow, unfiltered",
  207. rr: SingleRow("wmckinley"),
  208. want: "wmckinley-tjefferson-1",
  209. },
  210. {
  211. desc: "read all, with ColumnFilter",
  212. rr: RowRange{},
  213. filter: ColumnFilter(".*j.*"), // matches "jadams" and "tjefferson"
  214. want: "gwashington-jadams-1,jadams-tjefferson-1,tjefferson-jadams-1,wmckinley-tjefferson-1",
  215. },
  216. {
  217. desc: "read range, with ColumnRangeFilter",
  218. rr: RowRange{},
  219. filter: ColumnRangeFilter("follows", "h", "k"),
  220. want: "gwashington-jadams-1,tjefferson-jadams-1",
  221. },
  222. {
  223. desc: "read range from empty, with ColumnRangeFilter",
  224. rr: RowRange{},
  225. filter: ColumnRangeFilter("follows", "", "u"),
  226. want: "gwashington-jadams-1,jadams-gwashington-1,jadams-tjefferson-1,tjefferson-gwashington-1,tjefferson-jadams-1,wmckinley-tjefferson-1",
  227. },
  228. {
  229. desc: "read range from start to empty, with ColumnRangeFilter",
  230. rr: RowRange{},
  231. filter: ColumnRangeFilter("follows", "h", ""),
  232. want: "gwashington-jadams-1,jadams-tjefferson-1,tjefferson-jadams-1,tjefferson-wmckinley-1,wmckinley-tjefferson-1",
  233. },
  234. {
  235. desc: "read with RowKeyFilter",
  236. rr: RowRange{},
  237. filter: RowKeyFilter(".*wash.*"),
  238. want: "gwashington-jadams-1",
  239. },
  240. {
  241. desc: "read with RowKeyFilter, no matches",
  242. rr: RowRange{},
  243. filter: RowKeyFilter(".*xxx.*"),
  244. want: "",
  245. },
  246. {
  247. desc: "read with FamilyFilter, no matches",
  248. rr: RowRange{},
  249. filter: FamilyFilter(".*xxx.*"),
  250. want: "",
  251. },
  252. {
  253. desc: "read with ColumnFilter + row limit",
  254. rr: RowRange{},
  255. filter: ColumnFilter(".*j.*"), // matches "jadams" and "tjefferson"
  256. limit: LimitRows(2),
  257. want: "gwashington-jadams-1,jadams-tjefferson-1",
  258. },
  259. {
  260. desc: "read all, strip values",
  261. rr: RowRange{},
  262. filter: StripValueFilter(),
  263. want: "gwashington-jadams-,jadams-gwashington-,jadams-tjefferson-,tjefferson-gwashington-,tjefferson-jadams-,tjefferson-wmckinley-,wmckinley-tjefferson-",
  264. },
  265. {
  266. desc: "read with ColumnFilter + row limit + strip values",
  267. rr: RowRange{},
  268. filter: ChainFilters(ColumnFilter(".*j.*"), StripValueFilter()), // matches "jadams" and "tjefferson"
  269. limit: LimitRows(2),
  270. want: "gwashington-jadams-,jadams-tjefferson-",
  271. },
  272. {
  273. desc: "read with condition, strip values on true",
  274. rr: RowRange{},
  275. filter: ConditionFilter(ColumnFilter(".*j.*"), StripValueFilter(), nil),
  276. want: "gwashington-jadams-,jadams-gwashington-,jadams-tjefferson-,tjefferson-gwashington-,tjefferson-jadams-,tjefferson-wmckinley-,wmckinley-tjefferson-",
  277. },
  278. {
  279. desc: "read with condition, strip values on false",
  280. rr: RowRange{},
  281. filter: ConditionFilter(ColumnFilter(".*xxx.*"), nil, StripValueFilter()),
  282. want: "gwashington-jadams-,jadams-gwashington-,jadams-tjefferson-,tjefferson-gwashington-,tjefferson-jadams-,tjefferson-wmckinley-,wmckinley-tjefferson-",
  283. },
  284. {
  285. desc: "read with ValueRangeFilter + row limit",
  286. rr: RowRange{},
  287. filter: ValueRangeFilter([]byte("1"), []byte("5")), // matches our value of "1"
  288. limit: LimitRows(2),
  289. want: "gwashington-jadams-1,jadams-gwashington-1,jadams-tjefferson-1",
  290. },
  291. {
  292. desc: "read with ValueRangeFilter, no match on exclusive end",
  293. rr: RowRange{},
  294. filter: ValueRangeFilter([]byte("0"), []byte("1")), // no match
  295. want: "",
  296. },
  297. {
  298. desc: "read with ValueRangeFilter, no matches",
  299. rr: RowRange{},
  300. filter: ValueRangeFilter([]byte("3"), []byte("5")), // matches nothing
  301. want: "",
  302. },
  303. {
  304. desc: "read with InterleaveFilter, no matches on all filters",
  305. rr: RowRange{},
  306. filter: InterleaveFilters(ColumnFilter(".*x.*"), ColumnFilter(".*z.*")),
  307. want: "",
  308. },
  309. {
  310. desc: "read with InterleaveFilter, no duplicate cells",
  311. rr: RowRange{},
  312. filter: InterleaveFilters(ColumnFilter(".*g.*"), ColumnFilter(".*j.*")),
  313. want: "gwashington-jadams-1,jadams-gwashington-1,jadams-tjefferson-1,tjefferson-gwashington-1,tjefferson-jadams-1,wmckinley-tjefferson-1",
  314. },
  315. {
  316. desc: "read with InterleaveFilter, with duplicate cells",
  317. rr: RowRange{},
  318. filter: InterleaveFilters(ColumnFilter(".*g.*"), ColumnFilter(".*g.*")),
  319. want: "jadams-gwashington-1,jadams-gwashington-1,tjefferson-gwashington-1,tjefferson-gwashington-1",
  320. },
  321. {
  322. desc: "read with a RowRangeList and no filter",
  323. rr: RowRangeList{NewRange("gargamel", "hubbard"), InfiniteRange("wmckinley")},
  324. want: "gwashington-jadams-1,wmckinley-tjefferson-1",
  325. },
  326. {
  327. desc: "chain that excludes rows and matches nothing, in a condition",
  328. rr: RowRange{},
  329. filter: ConditionFilter(ChainFilters(ColumnFilter(".*j.*"), ColumnFilter(".*mckinley.*")), StripValueFilter(), nil),
  330. want: "",
  331. },
  332. {
  333. desc: "chain that ends with an interleave that has no match. covers #804",
  334. rr: RowRange{},
  335. filter: ConditionFilter(ChainFilters(ColumnFilter(".*j.*"), InterleaveFilters(ColumnFilter(".*x.*"), ColumnFilter(".*z.*"))), StripValueFilter(), nil),
  336. want: "",
  337. },
  338. }
  339. for _, tc := range readTests {
  340. var opts []ReadOption
  341. if tc.filter != nil {
  342. opts = append(opts, RowFilter(tc.filter))
  343. }
  344. if tc.limit != nil {
  345. opts = append(opts, tc.limit)
  346. }
  347. var elt []string
  348. err := tbl.ReadRows(context.Background(), tc.rr, func(r Row) bool {
  349. for _, ris := range r {
  350. for _, ri := range ris {
  351. elt = append(elt, formatReadItem(ri))
  352. }
  353. }
  354. return true
  355. }, opts...)
  356. if err != nil {
  357. t.Errorf("%s: %v", tc.desc, err)
  358. continue
  359. }
  360. if got := strings.Join(elt, ","); got != tc.want {
  361. t.Errorf("%s: wrong reads.\n got %q\nwant %q", tc.desc, got, tc.want)
  362. }
  363. }
  364. // Read a RowList
  365. var elt []string
  366. keys := RowList{"wmckinley", "gwashington", "jadams"}
  367. want := "gwashington-jadams-1,jadams-gwashington-1,jadams-tjefferson-1,wmckinley-tjefferson-1"
  368. err = tbl.ReadRows(ctx, keys, func(r Row) bool {
  369. for _, ris := range r {
  370. for _, ri := range ris {
  371. elt = append(elt, formatReadItem(ri))
  372. }
  373. }
  374. return true
  375. })
  376. if err != nil {
  377. t.Errorf("read RowList: %v", err)
  378. }
  379. if got := strings.Join(elt, ","); got != want {
  380. t.Errorf("bulk read: wrong reads.\n got %q\nwant %q", got, want)
  381. }
  382. checkpoint("tested ReadRows in a few ways")
  383. // Do a scan and stop part way through.
  384. // Verify that the ReadRows callback doesn't keep running.
  385. stopped := false
  386. err = tbl.ReadRows(ctx, InfiniteRange(""), func(r Row) bool {
  387. if r.Key() < "h" {
  388. return true
  389. }
  390. if !stopped {
  391. stopped = true
  392. return false
  393. }
  394. t.Errorf("ReadRows kept scanning to row %q after being told to stop", r.Key())
  395. return false
  396. })
  397. if err != nil {
  398. t.Errorf("Partial ReadRows: %v", err)
  399. }
  400. checkpoint("did partial ReadRows test")
  401. // Delete a row and check it goes away.
  402. mut = NewMutation()
  403. mut.DeleteRow()
  404. if err := tbl.Apply(ctx, "wmckinley", mut); err != nil {
  405. t.Errorf("Apply DeleteRow: %v", err)
  406. }
  407. row, err = tbl.ReadRow(ctx, "wmckinley")
  408. if err != nil {
  409. t.Fatalf("Reading a row after DeleteRow: %v", err)
  410. }
  411. if len(row) != 0 {
  412. t.Fatalf("Read non-zero row after DeleteRow: %v", row)
  413. }
  414. checkpoint("exercised DeleteRow")
  415. // Check ReadModifyWrite.
  416. if err := adminClient.CreateColumnFamily(ctx, table, "counter"); err != nil {
  417. t.Fatalf("Creating column family: %v", err)
  418. }
  419. appendRMW := func(b []byte) *ReadModifyWrite {
  420. rmw := NewReadModifyWrite()
  421. rmw.AppendValue("counter", "likes", b)
  422. return rmw
  423. }
  424. incRMW := func(n int64) *ReadModifyWrite {
  425. rmw := NewReadModifyWrite()
  426. rmw.Increment("counter", "likes", n)
  427. return rmw
  428. }
  429. rmwSeq := []struct {
  430. desc string
  431. rmw *ReadModifyWrite
  432. want []byte
  433. }{
  434. {
  435. desc: "append #1",
  436. rmw: appendRMW([]byte{0, 0, 0}),
  437. want: []byte{0, 0, 0},
  438. },
  439. {
  440. desc: "append #2",
  441. rmw: appendRMW([]byte{0, 0, 0, 0, 17}), // the remaining 40 bits to make a big-endian 17
  442. want: []byte{0, 0, 0, 0, 0, 0, 0, 17},
  443. },
  444. {
  445. desc: "increment",
  446. rmw: incRMW(8),
  447. want: []byte{0, 0, 0, 0, 0, 0, 0, 25},
  448. },
  449. }
  450. for _, step := range rmwSeq {
  451. row, err := tbl.ApplyReadModifyWrite(ctx, "gwashington", step.rmw)
  452. if err != nil {
  453. t.Fatalf("ApplyReadModifyWrite %+v: %v", step.rmw, err)
  454. }
  455. // Make sure the modified cell returned by the RMW operation has a timestamp.
  456. if row["counter"][0].Timestamp == 0 {
  457. t.Errorf("RMW returned cell timestamp: got %v, want > 0", row["counter"][0].Timestamp)
  458. }
  459. clearTimestamps(row)
  460. wantRow := Row{"counter": []ReadItem{{Row: "gwashington", Column: "counter:likes", Value: step.want}}}
  461. if !testutil.Equal(row, wantRow) {
  462. t.Fatalf("After %s,\n got %v\nwant %v", step.desc, row, wantRow)
  463. }
  464. }
  465. // Check for google-cloud-go/issues/723. RMWs that insert new rows should keep row order sorted in the emulator.
  466. row, err = tbl.ApplyReadModifyWrite(ctx, "issue-723-2", appendRMW([]byte{0}))
  467. if err != nil {
  468. t.Fatalf("ApplyReadModifyWrite null string: %v", err)
  469. }
  470. row, err = tbl.ApplyReadModifyWrite(ctx, "issue-723-1", appendRMW([]byte{0}))
  471. if err != nil {
  472. t.Fatalf("ApplyReadModifyWrite null string: %v", err)
  473. }
  474. // Get only the correct row back on read.
  475. r, err := tbl.ReadRow(ctx, "issue-723-1")
  476. if err != nil {
  477. t.Fatalf("Reading row: %v", err)
  478. }
  479. if r.Key() != "issue-723-1" {
  480. t.Errorf("ApplyReadModifyWrite: incorrect read after RMW,\n got %v\nwant %v", r.Key(), "issue-723-1")
  481. }
  482. checkpoint("tested ReadModifyWrite")
  483. // Test arbitrary timestamps more thoroughly.
  484. if err := adminClient.CreateColumnFamily(ctx, table, "ts"); err != nil {
  485. t.Fatalf("Creating column family: %v", err)
  486. }
  487. const numVersions = 4
  488. mut = NewMutation()
  489. for i := 1; i < numVersions; i++ {
  490. // Timestamps are used in thousands because the server
  491. // only permits that granularity.
  492. mut.Set("ts", "col", Timestamp(i*1000), []byte(fmt.Sprintf("val-%d", i)))
  493. mut.Set("ts", "col2", Timestamp(i*1000), []byte(fmt.Sprintf("val-%d", i)))
  494. }
  495. if err := tbl.Apply(ctx, "testrow", mut); err != nil {
  496. t.Fatalf("Mutating row: %v", err)
  497. }
  498. r, err = tbl.ReadRow(ctx, "testrow")
  499. if err != nil {
  500. t.Fatalf("Reading row: %v", err)
  501. }
  502. wantRow = Row{"ts": []ReadItem{
  503. // These should be returned in descending timestamp order.
  504. {Row: "testrow", Column: "ts:col", Timestamp: 3000, Value: []byte("val-3")},
  505. {Row: "testrow", Column: "ts:col", Timestamp: 2000, Value: []byte("val-2")},
  506. {Row: "testrow", Column: "ts:col", Timestamp: 1000, Value: []byte("val-1")},
  507. {Row: "testrow", Column: "ts:col2", Timestamp: 3000, Value: []byte("val-3")},
  508. {Row: "testrow", Column: "ts:col2", Timestamp: 2000, Value: []byte("val-2")},
  509. {Row: "testrow", Column: "ts:col2", Timestamp: 1000, Value: []byte("val-1")},
  510. }}
  511. if !testutil.Equal(r, wantRow) {
  512. t.Errorf("Cell with multiple versions,\n got %v\nwant %v", r, wantRow)
  513. }
  514. // Do the same read, but filter to the latest two versions.
  515. r, err = tbl.ReadRow(ctx, "testrow", RowFilter(LatestNFilter(2)))
  516. if err != nil {
  517. t.Fatalf("Reading row: %v", err)
  518. }
  519. wantRow = Row{"ts": []ReadItem{
  520. {Row: "testrow", Column: "ts:col", Timestamp: 3000, Value: []byte("val-3")},
  521. {Row: "testrow", Column: "ts:col", Timestamp: 2000, Value: []byte("val-2")},
  522. {Row: "testrow", Column: "ts:col2", Timestamp: 3000, Value: []byte("val-3")},
  523. {Row: "testrow", Column: "ts:col2", Timestamp: 2000, Value: []byte("val-2")},
  524. }}
  525. if !testutil.Equal(r, wantRow) {
  526. t.Errorf("Cell with multiple versions and LatestNFilter(2),\n got %v\nwant %v", r, wantRow)
  527. }
  528. // Check cell offset / limit
  529. r, err = tbl.ReadRow(ctx, "testrow", RowFilter(CellsPerRowLimitFilter(3)))
  530. if err != nil {
  531. t.Fatalf("Reading row: %v", err)
  532. }
  533. wantRow = Row{"ts": []ReadItem{
  534. {Row: "testrow", Column: "ts:col", Timestamp: 3000, Value: []byte("val-3")},
  535. {Row: "testrow", Column: "ts:col", Timestamp: 2000, Value: []byte("val-2")},
  536. {Row: "testrow", Column: "ts:col", Timestamp: 1000, Value: []byte("val-1")},
  537. }}
  538. if !testutil.Equal(r, wantRow) {
  539. t.Errorf("Cell with multiple versions and CellsPerRowLimitFilter(3),\n got %v\nwant %v", r, wantRow)
  540. }
  541. r, err = tbl.ReadRow(ctx, "testrow", RowFilter(CellsPerRowOffsetFilter(3)))
  542. if err != nil {
  543. t.Fatalf("Reading row: %v", err)
  544. }
  545. wantRow = Row{"ts": []ReadItem{
  546. {Row: "testrow", Column: "ts:col2", Timestamp: 3000, Value: []byte("val-3")},
  547. {Row: "testrow", Column: "ts:col2", Timestamp: 2000, Value: []byte("val-2")},
  548. {Row: "testrow", Column: "ts:col2", Timestamp: 1000, Value: []byte("val-1")},
  549. }}
  550. if !testutil.Equal(r, wantRow) {
  551. t.Errorf("Cell with multiple versions and CellsPerRowOffsetFilter(3),\n got %v\nwant %v", r, wantRow)
  552. }
  553. // Check timestamp range filtering (with truncation)
  554. r, err = tbl.ReadRow(ctx, "testrow", RowFilter(TimestampRangeFilterMicros(1001, 3000)))
  555. if err != nil {
  556. t.Fatalf("Reading row: %v", err)
  557. }
  558. wantRow = Row{"ts": []ReadItem{
  559. {Row: "testrow", Column: "ts:col", Timestamp: 2000, Value: []byte("val-2")},
  560. {Row: "testrow", Column: "ts:col", Timestamp: 1000, Value: []byte("val-1")},
  561. {Row: "testrow", Column: "ts:col2", Timestamp: 2000, Value: []byte("val-2")},
  562. {Row: "testrow", Column: "ts:col2", Timestamp: 1000, Value: []byte("val-1")},
  563. }}
  564. if !testutil.Equal(r, wantRow) {
  565. t.Errorf("Cell with multiple versions and TimestampRangeFilter(1000, 3000),\n got %v\nwant %v", r, wantRow)
  566. }
  567. r, err = tbl.ReadRow(ctx, "testrow", RowFilter(TimestampRangeFilterMicros(1000, 0)))
  568. if err != nil {
  569. t.Fatalf("Reading row: %v", err)
  570. }
  571. wantRow = Row{"ts": []ReadItem{
  572. {Row: "testrow", Column: "ts:col", Timestamp: 3000, Value: []byte("val-3")},
  573. {Row: "testrow", Column: "ts:col", Timestamp: 2000, Value: []byte("val-2")},
  574. {Row: "testrow", Column: "ts:col", Timestamp: 1000, Value: []byte("val-1")},
  575. {Row: "testrow", Column: "ts:col2", Timestamp: 3000, Value: []byte("val-3")},
  576. {Row: "testrow", Column: "ts:col2", Timestamp: 2000, Value: []byte("val-2")},
  577. {Row: "testrow", Column: "ts:col2", Timestamp: 1000, Value: []byte("val-1")},
  578. }}
  579. if !testutil.Equal(r, wantRow) {
  580. t.Errorf("Cell with multiple versions and TimestampRangeFilter(1000, 0),\n got %v\nwant %v", r, wantRow)
  581. }
  582. // Delete non-existing cells, no such column family in this row
  583. // Should not delete anything
  584. if err := adminClient.CreateColumnFamily(ctx, table, "non-existing"); err != nil {
  585. t.Fatalf("Creating column family: %v", err)
  586. }
  587. mut = NewMutation()
  588. mut.DeleteTimestampRange("non-existing", "col", 2000, 3000) // half-open interval
  589. if err := tbl.Apply(ctx, "testrow", mut); err != nil {
  590. t.Fatalf("Mutating row: %v", err)
  591. }
  592. r, err = tbl.ReadRow(ctx, "testrow", RowFilter(LatestNFilter(3)))
  593. if err != nil {
  594. t.Fatalf("Reading row: %v", err)
  595. }
  596. if !testutil.Equal(r, wantRow) {
  597. t.Errorf("Cell was deleted unexpectly,\n got %v\nwant %v", r, wantRow)
  598. }
  599. // Delete non-existing cells, no such column in this column family
  600. // Should not delete anything
  601. mut = NewMutation()
  602. mut.DeleteTimestampRange("ts", "non-existing", 2000, 3000) // half-open interval
  603. if err := tbl.Apply(ctx, "testrow", mut); err != nil {
  604. t.Fatalf("Mutating row: %v", err)
  605. }
  606. r, err = tbl.ReadRow(ctx, "testrow", RowFilter(LatestNFilter(3)))
  607. if err != nil {
  608. t.Fatalf("Reading row: %v", err)
  609. }
  610. if !testutil.Equal(r, wantRow) {
  611. t.Errorf("Cell was deleted unexpectly,\n got %v\nwant %v", r, wantRow)
  612. }
  613. // Delete the cell with timestamp 2000 and repeat the last read,
  614. // checking that we get ts 3000 and ts 1000.
  615. mut = NewMutation()
  616. mut.DeleteTimestampRange("ts", "col", 2001, 3000) // half-open interval
  617. if err := tbl.Apply(ctx, "testrow", mut); err != nil {
  618. t.Fatalf("Mutating row: %v", err)
  619. }
  620. r, err = tbl.ReadRow(ctx, "testrow", RowFilter(LatestNFilter(2)))
  621. if err != nil {
  622. t.Fatalf("Reading row: %v", err)
  623. }
  624. wantRow = Row{"ts": []ReadItem{
  625. {Row: "testrow", Column: "ts:col", Timestamp: 3000, Value: []byte("val-3")},
  626. {Row: "testrow", Column: "ts:col", Timestamp: 1000, Value: []byte("val-1")},
  627. {Row: "testrow", Column: "ts:col2", Timestamp: 3000, Value: []byte("val-3")},
  628. {Row: "testrow", Column: "ts:col2", Timestamp: 2000, Value: []byte("val-2")},
  629. }}
  630. if !testutil.Equal(r, wantRow) {
  631. t.Errorf("Cell with multiple versions and LatestNFilter(2), after deleting timestamp 2000,\n got %v\nwant %v", r, wantRow)
  632. }
  633. checkpoint("tested multiple versions in a cell")
  634. // Check DeleteCellsInFamily
  635. if err := adminClient.CreateColumnFamily(ctx, table, "status"); err != nil {
  636. t.Fatalf("Creating column family: %v", err)
  637. }
  638. mut = NewMutation()
  639. mut.Set("status", "start", 2000, []byte("2"))
  640. mut.Set("status", "end", 3000, []byte("3"))
  641. mut.Set("ts", "col", 1000, []byte("1"))
  642. if err := tbl.Apply(ctx, "row1", mut); err != nil {
  643. t.Errorf("Mutating row: %v", err)
  644. }
  645. if err := tbl.Apply(ctx, "row2", mut); err != nil {
  646. t.Errorf("Mutating row: %v", err)
  647. }
  648. mut = NewMutation()
  649. mut.DeleteCellsInFamily("status")
  650. if err := tbl.Apply(ctx, "row1", mut); err != nil {
  651. t.Errorf("Delete cf: %v", err)
  652. }
  653. // ColumnFamily removed
  654. r, err = tbl.ReadRow(ctx, "row1")
  655. if err != nil {
  656. t.Fatalf("Reading row: %v", err)
  657. }
  658. wantRow = Row{"ts": []ReadItem{
  659. {Row: "row1", Column: "ts:col", Timestamp: 1000, Value: []byte("1")},
  660. }}
  661. if !testutil.Equal(r, wantRow) {
  662. t.Errorf("column family was not deleted.\n got %v\n want %v", r, wantRow)
  663. }
  664. // ColumnFamily not removed
  665. r, err = tbl.ReadRow(ctx, "row2")
  666. if err != nil {
  667. t.Fatalf("Reading row: %v", err)
  668. }
  669. wantRow = Row{
  670. "ts": []ReadItem{
  671. {Row: "row2", Column: "ts:col", Timestamp: 1000, Value: []byte("1")},
  672. },
  673. "status": []ReadItem{
  674. {Row: "row2", Column: "status:end", Timestamp: 3000, Value: []byte("3")},
  675. {Row: "row2", Column: "status:start", Timestamp: 2000, Value: []byte("2")},
  676. },
  677. }
  678. if !testutil.Equal(r, wantRow) {
  679. t.Errorf("Column family was deleted unexpectly.\n got %v\n want %v", r, wantRow)
  680. }
  681. checkpoint("tested family delete")
  682. // Check DeleteCellsInColumn
  683. mut = NewMutation()
  684. mut.Set("status", "start", 2000, []byte("2"))
  685. mut.Set("status", "middle", 3000, []byte("3"))
  686. mut.Set("status", "end", 1000, []byte("1"))
  687. if err := tbl.Apply(ctx, "row3", mut); err != nil {
  688. t.Errorf("Mutating row: %v", err)
  689. }
  690. mut = NewMutation()
  691. mut.DeleteCellsInColumn("status", "middle")
  692. if err := tbl.Apply(ctx, "row3", mut); err != nil {
  693. t.Errorf("Delete column: %v", err)
  694. }
  695. r, err = tbl.ReadRow(ctx, "row3")
  696. if err != nil {
  697. t.Fatalf("Reading row: %v", err)
  698. }
  699. wantRow = Row{
  700. "status": []ReadItem{
  701. {Row: "row3", Column: "status:end", Timestamp: 1000, Value: []byte("1")},
  702. {Row: "row3", Column: "status:start", Timestamp: 2000, Value: []byte("2")},
  703. },
  704. }
  705. if !testutil.Equal(r, wantRow) {
  706. t.Errorf("Column was not deleted.\n got %v\n want %v", r, wantRow)
  707. }
  708. mut = NewMutation()
  709. mut.DeleteCellsInColumn("status", "start")
  710. if err := tbl.Apply(ctx, "row3", mut); err != nil {
  711. t.Errorf("Delete column: %v", err)
  712. }
  713. r, err = tbl.ReadRow(ctx, "row3")
  714. if err != nil {
  715. t.Fatalf("Reading row: %v", err)
  716. }
  717. wantRow = Row{
  718. "status": []ReadItem{
  719. {Row: "row3", Column: "status:end", Timestamp: 1000, Value: []byte("1")},
  720. },
  721. }
  722. if !testutil.Equal(r, wantRow) {
  723. t.Errorf("Column was not deleted.\n got %v\n want %v", r, wantRow)
  724. }
  725. mut = NewMutation()
  726. mut.DeleteCellsInColumn("status", "end")
  727. if err := tbl.Apply(ctx, "row3", mut); err != nil {
  728. t.Errorf("Delete column: %v", err)
  729. }
  730. r, err = tbl.ReadRow(ctx, "row3")
  731. if err != nil {
  732. t.Fatalf("Reading row: %v", err)
  733. }
  734. if len(r) != 0 {
  735. t.Errorf("Delete column: got %v, want empty row", r)
  736. }
  737. // Add same cell after delete
  738. mut = NewMutation()
  739. mut.Set("status", "end", 1000, []byte("1"))
  740. if err := tbl.Apply(ctx, "row3", mut); err != nil {
  741. t.Errorf("Mutating row: %v", err)
  742. }
  743. r, err = tbl.ReadRow(ctx, "row3")
  744. if err != nil {
  745. t.Fatalf("Reading row: %v", err)
  746. }
  747. if !testutil.Equal(r, wantRow) {
  748. t.Errorf("Column was not deleted correctly.\n got %v\n want %v", r, wantRow)
  749. }
  750. checkpoint("tested column delete")
  751. // Do highly concurrent reads/writes.
  752. // TODO(dsymonds): Raise this to 1000 when https://github.com/grpc/grpc-go/issues/205 is resolved.
  753. const maxConcurrency = 100
  754. var wg sync.WaitGroup
  755. for i := 0; i < maxConcurrency; i++ {
  756. wg.Add(1)
  757. go func() {
  758. defer wg.Done()
  759. switch r := rand.Intn(100); { // r ∈ [0,100)
  760. case 0 <= r && r < 30:
  761. // Do a read.
  762. _, err := tbl.ReadRow(ctx, "testrow", RowFilter(LatestNFilter(1)))
  763. if err != nil {
  764. t.Errorf("Concurrent read: %v", err)
  765. }
  766. case 30 <= r && r < 100:
  767. // Do a write.
  768. mut := NewMutation()
  769. mut.Set("ts", "col", 1000, []byte("data"))
  770. if err := tbl.Apply(ctx, "testrow", mut); err != nil {
  771. t.Errorf("Concurrent write: %v", err)
  772. }
  773. }
  774. }()
  775. }
  776. wg.Wait()
  777. checkpoint("tested high concurrency")
  778. // Large reads, writes and scans.
  779. bigBytes := make([]byte, 5<<20) // 5 MB is larger than current default gRPC max of 4 MB, but less than the max we set.
  780. nonsense := []byte("lorem ipsum dolor sit amet, ")
  781. fill(bigBytes, nonsense)
  782. mut = NewMutation()
  783. mut.Set("ts", "col", 1000, bigBytes)
  784. if err := tbl.Apply(ctx, "bigrow", mut); err != nil {
  785. t.Errorf("Big write: %v", err)
  786. }
  787. r, err = tbl.ReadRow(ctx, "bigrow")
  788. if err != nil {
  789. t.Errorf("Big read: %v", err)
  790. }
  791. wantRow = Row{"ts": []ReadItem{
  792. {Row: "bigrow", Column: "ts:col", Timestamp: 1000, Value: bigBytes},
  793. }}
  794. if !testutil.Equal(r, wantRow) {
  795. t.Errorf("Big read returned incorrect bytes: %v", r)
  796. }
  797. // Now write 1000 rows, each with 82 KB values, then scan them all.
  798. medBytes := make([]byte, 82<<10)
  799. fill(medBytes, nonsense)
  800. sem := make(chan int, 50) // do up to 50 mutations at a time.
  801. for i := 0; i < 1000; i++ {
  802. mut := NewMutation()
  803. mut.Set("ts", "big-scan", 1000, medBytes)
  804. row := fmt.Sprintf("row-%d", i)
  805. wg.Add(1)
  806. go func() {
  807. defer wg.Done()
  808. defer func() { <-sem }()
  809. sem <- 1
  810. if err := tbl.Apply(ctx, row, mut); err != nil {
  811. t.Errorf("Preparing large scan: %v", err)
  812. }
  813. }()
  814. }
  815. wg.Wait()
  816. n := 0
  817. err = tbl.ReadRows(ctx, PrefixRange("row-"), func(r Row) bool {
  818. for _, ris := range r {
  819. for _, ri := range ris {
  820. n += len(ri.Value)
  821. }
  822. }
  823. return true
  824. }, RowFilter(ColumnFilter("big-scan")))
  825. if err != nil {
  826. t.Errorf("Doing large scan: %v", err)
  827. }
  828. if want := 1000 * len(medBytes); n != want {
  829. t.Errorf("Large scan returned %d bytes, want %d", n, want)
  830. }
  831. // Scan a subset of the 1000 rows that we just created, using a LimitRows ReadOption.
  832. rc := 0
  833. wantRc := 3
  834. err = tbl.ReadRows(ctx, PrefixRange("row-"), func(r Row) bool {
  835. rc++
  836. return true
  837. }, LimitRows(int64(wantRc)))
  838. if rc != wantRc {
  839. t.Errorf("Scan with row limit returned %d rows, want %d", rc, wantRc)
  840. }
  841. checkpoint("tested big read/write/scan")
  842. // Test bulk mutations
  843. if err := adminClient.CreateColumnFamily(ctx, table, "bulk"); err != nil {
  844. t.Fatalf("Creating column family: %v", err)
  845. }
  846. bulkData := map[string][]string{
  847. "red sox": {"2004", "2007", "2013"},
  848. "patriots": {"2001", "2003", "2004", "2014"},
  849. "celtics": {"1981", "1984", "1986", "2008"},
  850. }
  851. var rowKeys []string
  852. var muts []*Mutation
  853. for row, ss := range bulkData {
  854. mut := NewMutation()
  855. for _, name := range ss {
  856. mut.Set("bulk", name, 1000, []byte("1"))
  857. }
  858. rowKeys = append(rowKeys, row)
  859. muts = append(muts, mut)
  860. }
  861. status, err := tbl.ApplyBulk(ctx, rowKeys, muts)
  862. if err != nil {
  863. t.Fatalf("Bulk mutating rows %q: %v", rowKeys, err)
  864. }
  865. if status != nil {
  866. t.Errorf("non-nil errors: %v", err)
  867. }
  868. checkpoint("inserted bulk data")
  869. // Read each row back
  870. for rowKey, ss := range bulkData {
  871. row, err := tbl.ReadRow(ctx, rowKey)
  872. if err != nil {
  873. t.Fatalf("Reading a bulk row: %v", err)
  874. }
  875. var wantItems []ReadItem
  876. for _, val := range ss {
  877. wantItems = append(wantItems, ReadItem{Row: rowKey, Column: "bulk:" + val, Timestamp: 1000, Value: []byte("1")})
  878. }
  879. wantRow := Row{"bulk": wantItems}
  880. if !testutil.Equal(row, wantRow) {
  881. t.Errorf("Read row mismatch.\n got %#v\nwant %#v", row, wantRow)
  882. }
  883. }
  884. checkpoint("tested reading from bulk insert")
  885. // Test bulk write errors.
  886. // Note: Setting timestamps as ServerTime makes sure the mutations are not retried on error.
  887. badMut := NewMutation()
  888. badMut.Set("badfamily", "col", ServerTime, nil)
  889. badMut2 := NewMutation()
  890. badMut2.Set("badfamily2", "goodcol", ServerTime, []byte("1"))
  891. status, err = tbl.ApplyBulk(ctx, []string{"badrow", "badrow2"}, []*Mutation{badMut, badMut2})
  892. if err != nil {
  893. t.Fatalf("Bulk mutating rows %q: %v", rowKeys, err)
  894. }
  895. if status == nil {
  896. t.Errorf("No errors for bad bulk mutation")
  897. } else if status[0] == nil || status[1] == nil {
  898. t.Errorf("No error for bad bulk mutation")
  899. }
  900. }
  901. type requestCountingInterceptor struct {
  902. grpc.ClientStream
  903. requestCallback func()
  904. }
  905. func (i *requestCountingInterceptor) SendMsg(m interface{}) error {
  906. i.requestCallback()
  907. return i.ClientStream.SendMsg(m)
  908. }
  909. func (i *requestCountingInterceptor) RecvMsg(m interface{}) error {
  910. return i.ClientStream.RecvMsg(m)
  911. }
  912. func requestCallback(callback func()) func(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string, streamer grpc.Streamer, opts ...grpc.CallOption) (grpc.ClientStream, error) {
  913. return func(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string, streamer grpc.Streamer, opts ...grpc.CallOption) (grpc.ClientStream, error) {
  914. clientStream, err := streamer(ctx, desc, cc, method, opts...)
  915. return &requestCountingInterceptor{
  916. ClientStream: clientStream,
  917. requestCallback: callback,
  918. }, err
  919. }
  920. }
  921. // TestReadRowsInvalidRowSet verifies that the client doesn't send ReadRows() requests with invalid RowSets.
  922. func TestReadRowsInvalidRowSet(t *testing.T) {
  923. testEnv, err := NewEmulatedEnv(IntegrationTestConfig{})
  924. if err != nil {
  925. t.Fatalf("NewEmulatedEnv failed: %v", err)
  926. }
  927. var requestCount int
  928. incrementRequestCount := func() { requestCount++ }
  929. conn, err := grpc.Dial(testEnv.server.Addr, grpc.WithInsecure(), grpc.WithBlock(),
  930. grpc.WithDefaultCallOptions(grpc.MaxCallSendMsgSize(100<<20), grpc.MaxCallRecvMsgSize(100<<20)),
  931. grpc.WithStreamInterceptor(requestCallback(incrementRequestCount)),
  932. )
  933. if err != nil {
  934. t.Fatalf("grpc.Dial failed: %v", err)
  935. }
  936. ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
  937. defer cancel()
  938. adminClient, err := NewAdminClient(ctx, testEnv.config.Project, testEnv.config.Instance, option.WithGRPCConn(conn))
  939. if err != nil {
  940. t.Fatalf("NewClient failed: %v", err)
  941. }
  942. defer adminClient.Close()
  943. if err := adminClient.CreateTable(ctx, testEnv.config.Table); err != nil {
  944. t.Fatalf("CreateTable(%v) failed: %v", testEnv.config.Table, err)
  945. }
  946. client, err := NewClient(ctx, testEnv.config.Project, testEnv.config.Instance, option.WithGRPCConn(conn))
  947. if err != nil {
  948. t.Fatalf("NewClient failed: %v", err)
  949. }
  950. defer client.Close()
  951. table := client.Open(testEnv.config.Table)
  952. tests := []struct {
  953. rr RowSet
  954. valid bool
  955. }{
  956. {
  957. rr: RowRange{},
  958. valid: true,
  959. },
  960. {
  961. rr: RowRange{start: "b"},
  962. valid: true,
  963. },
  964. {
  965. rr: RowRange{start: "b", limit: "c"},
  966. valid: true,
  967. },
  968. {
  969. rr: RowRange{start: "b", limit: "a"},
  970. valid: false,
  971. },
  972. {
  973. rr: RowList{"a"},
  974. valid: true,
  975. },
  976. {
  977. rr: RowList{},
  978. valid: false,
  979. },
  980. }
  981. for _, test := range tests {
  982. requestCount = 0
  983. err = table.ReadRows(ctx, test.rr, func(r Row) bool { return true })
  984. if err != nil {
  985. t.Fatalf("ReadRows(%v) failed: %v", test.rr, err)
  986. }
  987. requestValid := requestCount != 0
  988. if requestValid != test.valid {
  989. t.Errorf("%s: got %v, want %v", test.rr, requestValid, test.valid)
  990. }
  991. }
  992. }
  993. func formatReadItem(ri ReadItem) string {
  994. // Use the column qualifier only to make the test data briefer.
  995. col := ri.Column[strings.Index(ri.Column, ":")+1:]
  996. return fmt.Sprintf("%s-%s-%s", ri.Row, col, ri.Value)
  997. }
  998. func fill(b, sub []byte) {
  999. for len(b) > len(sub) {
  1000. n := copy(b, sub)
  1001. b = b[n:]
  1002. }
  1003. }
  1004. func clearTimestamps(r Row) {
  1005. for _, ris := range r {
  1006. for i := range ris {
  1007. ris[i].Timestamp = 0
  1008. }
  1009. }
  1010. }
  1011. func TestSampleRowKeys(t *testing.T) {
  1012. start := time.Now()
  1013. lastCheckpoint := start
  1014. checkpoint := func(s string) {
  1015. n := time.Now()
  1016. t.Logf("[%s] %v since start, %v since last checkpoint", s, n.Sub(start), n.Sub(lastCheckpoint))
  1017. lastCheckpoint = n
  1018. }
  1019. ctx := context.Background()
  1020. client, adminClient, table, err := doSetup(ctx)
  1021. if err != nil {
  1022. t.Fatalf("%v", err)
  1023. }
  1024. defer client.Close()
  1025. defer adminClient.Close()
  1026. tbl := client.Open(table)
  1027. // Delete the table at the end of the test.
  1028. // Do this even before creating the table so that if this is running
  1029. // against production and CreateTable fails there's a chance of cleaning it up.
  1030. defer adminClient.DeleteTable(ctx, table)
  1031. // Insert some data.
  1032. initialData := map[string][]string{
  1033. "wmckinley11": {"tjefferson11"},
  1034. "gwashington77": {"jadams77"},
  1035. "tjefferson0": {"gwashington0", "jadams0"},
  1036. }
  1037. for row, ss := range initialData {
  1038. mut := NewMutation()
  1039. for _, name := range ss {
  1040. mut.Set("follows", name, 1000, []byte("1"))
  1041. }
  1042. if err := tbl.Apply(ctx, row, mut); err != nil {
  1043. t.Errorf("Mutating row %q: %v", row, err)
  1044. }
  1045. }
  1046. checkpoint("inserted initial data")
  1047. sampleKeys, err := tbl.SampleRowKeys(context.Background())
  1048. if err != nil {
  1049. t.Errorf("%s: %v", "SampleRowKeys:", err)
  1050. }
  1051. if len(sampleKeys) == 0 {
  1052. t.Error("SampleRowKeys length 0")
  1053. }
  1054. checkpoint("tested SampleRowKeys.")
  1055. }
  1056. func doSetup(ctx context.Context) (*Client, *AdminClient, string, error) {
  1057. start := time.Now()
  1058. lastCheckpoint := start
  1059. checkpoint := func(s string) {
  1060. n := time.Now()
  1061. fmt.Printf("[%s] %v since start, %v since last checkpoint", s, n.Sub(start), n.Sub(lastCheckpoint))
  1062. lastCheckpoint = n
  1063. }
  1064. testEnv, err := NewIntegrationEnv()
  1065. if err != nil {
  1066. return nil, nil, "", fmt.Errorf("IntegrationEnv: %v", err)
  1067. }
  1068. var timeout time.Duration
  1069. if testEnv.Config().UseProd {
  1070. timeout = 10 * time.Minute
  1071. fmt.Printf("Running test against production")
  1072. } else {
  1073. timeout = 1 * time.Minute
  1074. fmt.Printf("bttest.Server running on %s", testEnv.Config().AdminEndpoint)
  1075. }
  1076. ctx, cancel := context.WithTimeout(ctx, timeout)
  1077. defer cancel()
  1078. client, err := testEnv.NewClient()
  1079. if err != nil {
  1080. return nil, nil, "", fmt.Errorf("Client: %v", err)
  1081. }
  1082. checkpoint("dialed Client")
  1083. adminClient, err := testEnv.NewAdminClient()
  1084. if err != nil {
  1085. return nil, nil, "", fmt.Errorf("AdminClient: %v", err)
  1086. }
  1087. checkpoint("dialed AdminClient")
  1088. table := testEnv.Config().Table
  1089. if err := adminClient.CreateTable(ctx, table); err != nil {
  1090. return nil, nil, "", fmt.Errorf("Creating table: %v", err)
  1091. }
  1092. checkpoint("created table")
  1093. if err := adminClient.CreateColumnFamily(ctx, table, "follows"); err != nil {
  1094. return nil, nil, "", fmt.Errorf("Creating column family: %v", err)
  1095. }
  1096. checkpoint(`created "follows" column family`)
  1097. return client, adminClient, table, nil
  1098. }