clear_scroll_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2012-present Oliver Eilhard. All rights reserved.
  2. // Use of this source code is governed by a MIT-license.
  3. // See http://olivere.mit-license.org/license.txt for details.
  4. package elastic
  5. import (
  6. "context"
  7. _ "net/http"
  8. "testing"
  9. )
  10. func TestClearScroll(t *testing.T) {
  11. client := setupTestClientAndCreateIndex(t)
  12. // client := setupTestClientAndCreateIndex(t, SetTraceLog(log.New(os.Stdout, "", log.LstdFlags)))
  13. tweet1 := tweet{User: "olivere", Message: "Welcome to Golang and Elasticsearch."}
  14. tweet2 := tweet{User: "olivere", Message: "Another unrelated topic."}
  15. tweet3 := tweet{User: "sandrae", Message: "Cycling is fun."}
  16. // Add all documents
  17. _, err := client.Index().Index(testIndexName).Type("tweet").Id("1").BodyJson(&tweet1).Do(context.TODO())
  18. if err != nil {
  19. t.Fatal(err)
  20. }
  21. _, err = client.Index().Index(testIndexName).Type("tweet").Id("2").BodyJson(&tweet2).Do(context.TODO())
  22. if err != nil {
  23. t.Fatal(err)
  24. }
  25. _, err = client.Index().Index(testIndexName).Type("tweet").Id("3").BodyJson(&tweet3).Do(context.TODO())
  26. if err != nil {
  27. t.Fatal(err)
  28. }
  29. _, err = client.Flush().Index(testIndexName).Do(context.TODO())
  30. if err != nil {
  31. t.Fatal(err)
  32. }
  33. // Match all should return all documents
  34. res, err := client.Scroll(testIndexName).Size(1).Do(context.TODO())
  35. if err != nil {
  36. t.Fatal(err)
  37. }
  38. if res == nil {
  39. t.Fatal("expected results != nil; got nil")
  40. }
  41. if res.ScrollId == "" {
  42. t.Fatalf("expected scrollId in results; got %q", res.ScrollId)
  43. }
  44. // Search should succeed
  45. _, err = client.Scroll(testIndexName).Size(1).ScrollId(res.ScrollId).Do(context.TODO())
  46. if err != nil {
  47. t.Fatal(err)
  48. }
  49. // Clear scroll id
  50. clearScrollRes, err := client.ClearScroll().ScrollId(res.ScrollId).Do(context.TODO())
  51. if err != nil {
  52. t.Fatal(err)
  53. }
  54. if clearScrollRes == nil {
  55. t.Fatal("expected results != nil; got nil")
  56. }
  57. // Search result should fail
  58. _, err = client.Scroll(testIndexName).Size(1).ScrollId(res.ScrollId).Do(context.TODO())
  59. if err == nil {
  60. t.Fatalf("expected scroll to fail")
  61. }
  62. }
  63. func TestClearScrollValidate(t *testing.T) {
  64. client := setupTestClient(t)
  65. // No scroll id -> fail with error
  66. res, err := NewClearScrollService(client).Do(context.TODO())
  67. if err == nil {
  68. t.Fatalf("expected ClearScroll to fail without scroll ids")
  69. }
  70. if res != nil {
  71. t.Fatalf("expected result to be nil; got: %v", res)
  72. }
  73. }