-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
35 changed files
with
1,667 additions
and
6,737 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,109 +1,47 @@ | ||
package buffer | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"sync" | ||
"testing" | ||
"time" | ||
|
||
"github.com/observiq/stanza/entry" | ||
"github.com/observiq/stanza/operator" | ||
"github.com/stretchr/testify/require" | ||
"go.uber.org/zap" | ||
yaml "gopkg.in/yaml.v2" | ||
) | ||
|
||
type bufferHandler struct { | ||
flushed []*entry.Entry | ||
mux sync.Mutex | ||
notify chan struct{} | ||
} | ||
|
||
func (b *bufferHandler) ProcessMulti(ctx context.Context, entries []*entry.Entry) error { | ||
b.mux.Lock() | ||
b.flushed = append(b.flushed, entries...) | ||
b.mux.Unlock() | ||
b.notify <- struct{}{} | ||
return nil | ||
} | ||
|
||
func (b *bufferHandler) Logger() *zap.SugaredLogger { | ||
return nil | ||
} | ||
|
||
func TestBuffer(t *testing.T) { | ||
config := NewConfig() | ||
config.DelayThreshold = operator.Duration{ | ||
Duration: 100 * time.Millisecond, | ||
} | ||
|
||
buf := NewMemoryBuffer(&config) | ||
numEntries := 10000 | ||
|
||
bh := bufferHandler{ | ||
flushed: make([]*entry.Entry, 0), | ||
notify: make(chan struct{}), | ||
} | ||
buf.SetHandler(&bh) | ||
|
||
for i := 0; i < numEntries; i++ { | ||
err := buf.AddWait(context.Background(), entry.New(), 0) | ||
require.NoError(t, err) | ||
} | ||
|
||
for { | ||
select { | ||
case <-bh.notify: | ||
bh.mux.Lock() | ||
if len(bh.flushed) == numEntries { | ||
bh.mux.Unlock() | ||
return | ||
} | ||
bh.mux.Unlock() | ||
case <-time.After(time.Second): | ||
require.FailNow(t, "timed out waiting for all entries to be flushed") | ||
} | ||
} | ||
} | ||
|
||
func TestBufferSerializationRoundtrip(t *testing.T) { | ||
func TestBufferUnmarshalYAML(t *testing.T) { | ||
cases := []struct { | ||
name string | ||
config Config | ||
name string | ||
input []byte | ||
expected Config | ||
}{ | ||
{ | ||
"zeros", | ||
Config{}, | ||
"SimpleMemory", | ||
[]byte("type: memory\nmax_entries: 30\n"), | ||
Config{ | ||
Type: "memory", | ||
BufferBuilder: &MemoryBufferConfig{ | ||
MaxEntries: 30, | ||
}, | ||
}, | ||
}, | ||
{ | ||
"defaults", | ||
NewConfig(), | ||
"SimpleDisk", | ||
[]byte("type: disk\nmax_size: 1234\npath: /var/log/testpath\n"), | ||
Config{ | ||
Type: "disk", | ||
BufferBuilder: &DiskBufferConfig{ | ||
MaxSize: 1234, | ||
Path: "/var/log/testpath", | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
t.Run("yaml "+tc.name, func(t *testing.T) { | ||
cfgBytes, err := yaml.Marshal(tc.config) | ||
require.NoError(t, err) | ||
|
||
var cfg Config | ||
err = yaml.UnmarshalStrict(cfgBytes, &cfg) | ||
require.NoError(t, err) | ||
|
||
require.Equal(t, tc.config, cfg) | ||
}) | ||
|
||
t.Run("json "+tc.name, func(t *testing.T) { | ||
tc := tc | ||
cfgBytes, err := json.Marshal(tc.config) | ||
t.Run(tc.name, func(t *testing.T) { | ||
var b Config | ||
err := yaml.Unmarshal(tc.input, &b) | ||
require.NoError(t, err) | ||
|
||
var cfg Config | ||
err = json.Unmarshal(cfgBytes, &cfg) | ||
require.NoError(t, err) | ||
|
||
require.Equal(t, tc.config, cfg) | ||
require.Equal(t, tc.expected, b) | ||
}) | ||
} | ||
} |
Oops, something went wrong.