|
| 1 | +/* |
| 2 | + * Copyright 2023 SentinelOne, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package buffer_config |
| 17 | + |
| 18 | +import ( |
| 19 | + "os" |
| 20 | + "testing" |
| 21 | + "time" |
| 22 | + |
| 23 | + "github.com/maxatome/go-testdeep/helpers/tdsuite" |
| 24 | + "github.com/maxatome/go-testdeep/td" |
| 25 | +) |
| 26 | + |
| 27 | +type SuiteBufferSettings struct{} |
| 28 | + |
| 29 | +func (s *SuiteBufferSettings) PreTest(t *td.T, testName string) error { |
| 30 | + os.Clearenv() |
| 31 | + return nil |
| 32 | +} |
| 33 | + |
| 34 | +func (s *SuiteBufferSettings) PostTest(t *td.T, testName string) error { |
| 35 | + os.Clearenv() |
| 36 | + return nil |
| 37 | +} |
| 38 | + |
| 39 | +func (s *SuiteBufferSettings) Destroy(t *td.T) error { |
| 40 | + os.Clearenv() |
| 41 | + return nil |
| 42 | +} |
| 43 | + |
| 44 | +func TestSuiteBufferSettings(t *testing.T) { |
| 45 | + td.NewT(t) |
| 46 | + tdsuite.Run(t, &SuiteBufferSettings{}) |
| 47 | +} |
| 48 | + |
| 49 | +func (s *SuiteBufferSettings) TestConfigWithOptions(assert, require *td.T) { |
| 50 | + bufCfg, errB := New( |
| 51 | + WithMaxLifetime(3*time.Second), |
| 52 | + WithMaxSize(12345), |
| 53 | + WithGroupBy([]string{"aaa", "bbb"}), |
| 54 | + WithRetryInitialInterval(8*time.Second), |
| 55 | + WithRetryMaxInterval(30*time.Second), |
| 56 | + WithRetryMaxElapsedTime(10*time.Minute), |
| 57 | + ) |
| 58 | + |
| 59 | + assert.CmpNoError(errB) |
| 60 | + |
| 61 | + assert.Cmp(*bufCfg, DataSetBufferSettings{ |
| 62 | + MaxLifetime: 3 * time.Second, |
| 63 | + MaxSize: 12345, |
| 64 | + GroupBy: []string{"aaa", "bbb"}, |
| 65 | + RetryInitialInterval: 8 * time.Second, |
| 66 | + RetryMaxInterval: 30 * time.Second, |
| 67 | + RetryMaxElapsedTime: 10 * time.Minute, |
| 68 | + }) |
| 69 | +} |
| 70 | + |
| 71 | +func (s *SuiteBufferSettings) TestDataConfigUpdate(assert, require *td.T) { |
| 72 | + bufCfg, errB := New( |
| 73 | + WithMaxLifetime(3*time.Second), |
| 74 | + WithMaxSize(12345), |
| 75 | + WithGroupBy([]string{"aaa", "bbb"}), |
| 76 | + WithRetryInitialInterval(8*time.Second), |
| 77 | + WithRetryMaxInterval(30*time.Second), |
| 78 | + WithRetryMaxElapsedTime(10*time.Minute), |
| 79 | + ) |
| 80 | + assert.CmpNoError(errB) |
| 81 | + |
| 82 | + assert.Cmp(*bufCfg, DataSetBufferSettings{ |
| 83 | + MaxLifetime: 3 * time.Second, |
| 84 | + MaxSize: 12345, |
| 85 | + GroupBy: []string{"aaa", "bbb"}, |
| 86 | + RetryInitialInterval: 8 * time.Second, |
| 87 | + RetryMaxInterval: 30 * time.Second, |
| 88 | + RetryMaxElapsedTime: 10 * time.Minute, |
| 89 | + }) |
| 90 | + |
| 91 | + bufCfg2, err := bufCfg.Update( |
| 92 | + WithMaxLifetime(23*time.Second), |
| 93 | + WithMaxSize(212345), |
| 94 | + WithGroupBy([]string{"2aaa", "2bbb"}), |
| 95 | + WithRetryInitialInterval(28*time.Second), |
| 96 | + WithRetryMaxInterval(230*time.Second), |
| 97 | + WithRetryMaxElapsedTime(210*time.Minute), |
| 98 | + ) |
| 99 | + assert.CmpNoError(err) |
| 100 | + |
| 101 | + // original config is unchanged |
| 102 | + assert.Cmp(*bufCfg, DataSetBufferSettings{ |
| 103 | + MaxLifetime: 3 * time.Second, |
| 104 | + MaxSize: 12345, |
| 105 | + GroupBy: []string{"aaa", "bbb"}, |
| 106 | + RetryInitialInterval: 8 * time.Second, |
| 107 | + RetryMaxInterval: 30 * time.Second, |
| 108 | + RetryMaxElapsedTime: 10 * time.Minute, |
| 109 | + }) |
| 110 | + |
| 111 | + // new config is changed |
| 112 | + assert.Cmp(*bufCfg2, DataSetBufferSettings{ |
| 113 | + MaxLifetime: 23 * time.Second, |
| 114 | + MaxSize: 212345, |
| 115 | + GroupBy: []string{"2aaa", "2bbb"}, |
| 116 | + RetryInitialInterval: 28 * time.Second, |
| 117 | + RetryMaxInterval: 230 * time.Second, |
| 118 | + RetryMaxElapsedTime: 210 * time.Minute, |
| 119 | + }) |
| 120 | +} |
0 commit comments