|
| 1 | +package handler_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/gol4ng/logger" |
| 7 | + "github.com/gol4ng/logger/handler" |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | +) |
| 10 | + |
| 11 | +func TestMemory_Handle(t *testing.T) { |
| 12 | + entry := logger.Entry{} |
| 13 | + |
| 14 | + h := handler.NewMemory() |
| 15 | + |
| 16 | + err := h.Handle(entry) |
| 17 | + assert.NoError(t, err) |
| 18 | + |
| 19 | + entries := h.GetEntries() |
| 20 | + assert.Len(t, entries, 1) |
| 21 | + |
| 22 | + assert.Equal(t, entry, entries[0]) |
| 23 | +} |
| 24 | + |
| 25 | +func TestMemory_HandleRace(t *testing.T) { |
| 26 | + h := handler.NewMemory() |
| 27 | + |
| 28 | + go func() { |
| 29 | + err := h.Handle(logger.Entry{}) |
| 30 | + assert.NoError(t, err) |
| 31 | + }() |
| 32 | + |
| 33 | + go func() { |
| 34 | + h.GetEntries() |
| 35 | + }() |
| 36 | +} |
| 37 | + |
| 38 | +func TestNewLogger_CleanEntries(t *testing.T) { |
| 39 | + entry := logger.Entry{} |
| 40 | + |
| 41 | + h := handler.NewMemory() |
| 42 | + |
| 43 | + err := h.Handle(entry) |
| 44 | + assert.NoError(t, err) |
| 45 | + |
| 46 | + entries := h.GetEntries() |
| 47 | + assert.Len(t, entries, 1) |
| 48 | + |
| 49 | + assert.Equal(t, entry, entries[0]) |
| 50 | + |
| 51 | + h.CleanEntries() |
| 52 | + |
| 53 | + assert.Empty(t, h.GetEntries()) |
| 54 | +} |
| 55 | + |
| 56 | +func TestNewLogger_GetAndCleanEntries(t *testing.T) { |
| 57 | + entry := logger.Entry{} |
| 58 | + |
| 59 | + h := handler.NewMemory() |
| 60 | + |
| 61 | + err := h.Handle(entry) |
| 62 | + assert.NoError(t, err) |
| 63 | + |
| 64 | + entries := h.GetAndCleanEntries() |
| 65 | + assert.Len(t, entries, 1) |
| 66 | + |
| 67 | + assert.Equal(t, entry, entries[0]) |
| 68 | + |
| 69 | + assert.Empty(t, h.GetEntries()) |
| 70 | +} |
0 commit comments