options_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Copyright 2020-2021 InfluxData, Inc. All rights reserved.
  2. // Use of this source code is governed by MIT
  3. // license that can be found in the LICENSE file.
  4. package influxdb2_test
  5. import (
  6. "context"
  7. "crypto/tls"
  8. "net/http"
  9. "net/http/httptest"
  10. "strings"
  11. "testing"
  12. "time"
  13. influxdb2 "github.com/influxdata/influxdb-client-go/v2"
  14. "github.com/stretchr/testify/assert"
  15. "github.com/stretchr/testify/require"
  16. )
  17. func TestDefaultOptions(t *testing.T) {
  18. opts := influxdb2.DefaultOptions()
  19. assert.EqualValues(t, 5_000, opts.BatchSize())
  20. assert.EqualValues(t, false, opts.UseGZip())
  21. assert.EqualValues(t, 1_000, opts.FlushInterval())
  22. assert.EqualValues(t, time.Nanosecond, opts.Precision())
  23. assert.EqualValues(t, 50_000, opts.RetryBufferLimit())
  24. assert.EqualValues(t, 5_000, opts.RetryInterval())
  25. assert.EqualValues(t, 5, opts.MaxRetries())
  26. assert.EqualValues(t, 125_000, opts.MaxRetryInterval())
  27. assert.EqualValues(t, 180_000, opts.MaxRetryTime())
  28. assert.EqualValues(t, 2, opts.ExponentialBase())
  29. assert.EqualValues(t, (*tls.Config)(nil), opts.TLSConfig())
  30. assert.EqualValues(t, 20, opts.HTTPRequestTimeout())
  31. assert.EqualValues(t, 0, opts.LogLevel())
  32. assert.EqualValues(t, "", opts.ApplicationName())
  33. }
  34. func TestSettingsOptions(t *testing.T) {
  35. tlsConfig := &tls.Config{
  36. InsecureSkipVerify: true,
  37. }
  38. opts := influxdb2.DefaultOptions().
  39. SetBatchSize(5).
  40. SetUseGZip(true).
  41. SetFlushInterval(5_000).
  42. SetPrecision(time.Millisecond).
  43. SetRetryBufferLimit(5).
  44. SetRetryInterval(1_000).
  45. SetMaxRetryInterval(10_000).
  46. SetMaxRetries(7).
  47. SetMaxRetryTime(500_000).
  48. SetExponentialBase(5).
  49. SetTLSConfig(tlsConfig).
  50. SetHTTPRequestTimeout(50).
  51. SetLogLevel(3).
  52. AddDefaultTag("t", "a").
  53. SetApplicationName("Monitor/1.1")
  54. assert.EqualValues(t, 5, opts.BatchSize())
  55. assert.EqualValues(t, true, opts.UseGZip())
  56. assert.EqualValues(t, 5_000, opts.FlushInterval())
  57. assert.EqualValues(t, time.Millisecond, opts.Precision())
  58. assert.EqualValues(t, 5, opts.RetryBufferLimit())
  59. assert.EqualValues(t, 1_000, opts.RetryInterval())
  60. assert.EqualValues(t, 10_000, opts.MaxRetryInterval())
  61. assert.EqualValues(t, 7, opts.MaxRetries())
  62. assert.EqualValues(t, 500_000, opts.MaxRetryTime())
  63. assert.EqualValues(t, 5, opts.ExponentialBase())
  64. assert.EqualValues(t, tlsConfig, opts.TLSConfig())
  65. assert.EqualValues(t, 50, opts.HTTPRequestTimeout())
  66. assert.EqualValues(t, "Monitor/1.1", opts.ApplicationName())
  67. if client := opts.HTTPClient(); assert.NotNil(t, client) {
  68. assert.EqualValues(t, 50*time.Second, client.Timeout)
  69. assert.Equal(t, tlsConfig, client.Transport.(*http.Transport).TLSClientConfig)
  70. }
  71. assert.EqualValues(t, 3, opts.LogLevel())
  72. assert.Len(t, opts.WriteOptions().DefaultTags(), 1)
  73. client := &http.Client{
  74. Transport: &http.Transport{},
  75. }
  76. opts.SetHTTPClient(client)
  77. assert.Equal(t, client, opts.HTTPClient())
  78. }
  79. func TestTimeout(t *testing.T) {
  80. response := `,result,table,_start,_stop,_time,_value,_field,_measurement,a,b,
  81. ,,0,2020-02-17T22:19:49.747562847Z,2020-02-18T22:19:49.747562847Z,2020-02-18T10:34:08.135814545Z,1.4,f,test,1,adsfasdf
  82. ,,0,2020-02-17T22:19:49.747562847Z,2020-02-18T22:19:49.747562847Z,2020-02-18T22:08:44.850214724Z,6.6,f,test,1,adsfasdf
  83. `
  84. server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  85. <-time.After(100 * time.Millisecond)
  86. if r.Method == http.MethodPost {
  87. w.Header().Set("Content-Type", "text/csv")
  88. w.WriteHeader(http.StatusOK)
  89. <-time.After(2 * time.Second)
  90. _, err := w.Write([]byte(response))
  91. if err != nil {
  92. w.WriteHeader(http.StatusInternalServerError)
  93. _, _ = w.Write([]byte(err.Error()))
  94. }
  95. } else {
  96. w.WriteHeader(http.StatusNotFound)
  97. }
  98. }))
  99. defer server.Close()
  100. client := influxdb2.NewClientWithOptions(server.URL, "a", influxdb2.DefaultOptions().SetHTTPRequestTimeout(1))
  101. queryAPI := client.QueryAPI("org")
  102. _, err := queryAPI.QueryRaw(context.Background(), "flux", nil)
  103. require.NotNil(t, err)
  104. assert.True(t, strings.Contains(err.Error(), "Client.Timeout exceeded"))
  105. client = influxdb2.NewClientWithOptions(server.URL, "a", influxdb2.DefaultOptions().SetHTTPRequestTimeout(5))
  106. queryAPI = client.QueryAPI("org")
  107. result, err := queryAPI.QueryRaw(context.Background(), "flux", nil)
  108. require.Nil(t, err)
  109. assert.Equal(t, response, result)
  110. }