Skip to content

Commit 592cd35

Browse files
Memory handler (#71)
1 parent af9638c commit 592cd35

File tree

3 files changed

+125
-42
lines changed

3 files changed

+125
-42
lines changed

handler/memory.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package handler
2+
3+
import (
4+
"sync"
5+
6+
"github.com/gol4ng/logger"
7+
)
8+
9+
// Memory will store Entry to slice entries
10+
// mainly develop for testing purpose in order to do some entries assertion
11+
type Memory struct {
12+
mu sync.Mutex
13+
entries []logger.Entry
14+
}
15+
16+
func (s *Memory) lock() func() {
17+
s.mu.Lock()
18+
return func() { s.mu.Unlock() }
19+
}
20+
21+
// Handle act as logger.HandlerInterface
22+
func (s *Memory) Handle(entry logger.Entry) error {
23+
defer s.lock()()
24+
s.entries = append(s.entries, entry)
25+
return nil
26+
}
27+
28+
// CleanEntries will reset the in memory entries list
29+
func (s *Memory) CleanEntries() {
30+
defer s.lock()()
31+
s.entries = []logger.Entry{}
32+
}
33+
34+
// GetEntries will return the in memory entries list
35+
func (s *Memory) GetEntries() []logger.Entry {
36+
defer s.lock()()
37+
return s.entries
38+
}
39+
40+
// GetAndCleanEntries will return and clean the in memory entries list
41+
func (s *Memory) GetAndCleanEntries() []logger.Entry {
42+
defer s.lock()()
43+
entries := s.entries
44+
s.entries = []logger.Entry{}
45+
return entries
46+
}
47+
48+
// NewMemory init a new Memory handler
49+
func NewMemory() *Memory {
50+
return &Memory{}
51+
}

handler/memory_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
}

testing/logger.go

Lines changed: 4 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,12 @@
11
package testing
22

33
import (
4-
"sync"
5-
64
"github.com/gol4ng/logger"
5+
"github.com/gol4ng/logger/handler"
76
)
87

9-
type inMemoryEntriesStore struct {
10-
mu sync.Mutex
11-
entries []logger.Entry
12-
}
13-
14-
func (s *inMemoryEntriesStore) lock() func() {
15-
s.mu.Lock()
16-
return func() { s.mu.Unlock() }
17-
}
18-
19-
// Handle act as logger.HandlerInterface
20-
func (s *inMemoryEntriesStore) Handle(entry logger.Entry) error {
21-
defer s.lock()()
22-
s.entries = append(s.entries, entry)
23-
return nil
24-
}
25-
26-
// CleanEntries will reset the in memory entries list
27-
func (s *inMemoryEntriesStore) CleanEntries() {
28-
defer s.lock()()
29-
s.entries = []logger.Entry{}
30-
}
31-
32-
// GetEntries will return the in memory entries list
33-
func (s *inMemoryEntriesStore) GetEntries() []logger.Entry {
34-
defer s.lock()()
35-
return s.entries
36-
}
37-
38-
// GetAndCleanEntries will return and clean the in memory entries list
39-
func (s *inMemoryEntriesStore) GetAndCleanEntries() []logger.Entry {
40-
defer s.lock()()
41-
entries := s.entries
42-
s.entries = []logger.Entry{}
43-
return entries
44-
}
45-
46-
// NewLogger will return a new inMemory Logger in order to do some assertion during test
47-
func NewLogger() (*logger.Logger, *inMemoryEntriesStore) {
48-
store := &inMemoryEntriesStore{}
8+
// NewLogger will return a new Memory Logger in order to do some assertion during test
9+
func NewLogger() (*logger.Logger, *handler.Memory) {
10+
store := handler.NewMemory()
4911
return logger.NewLogger(store.Handle), store
5012
}

0 commit comments

Comments
 (0)