Skip to content

Commit

Permalink
Add mutex writer wrapper for logger.
Browse files Browse the repository at this point in the history
  • Loading branch information
edoger committed Mar 21, 2022
1 parent f54f5e1 commit fcac028
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
40 changes: 40 additions & 0 deletions mutex_writer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2022 The ZKits Project Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package logger

import (
"io"
"sync"
)

// NewMutexWriter creates and returns a mutex writer.
// Mutually exclusive writers ensure mutual exclusion of individual write calls.
func NewMutexWriter(w io.Writer) io.Writer {
return &mutexWriter{w: w}
}

// This is an implementation of the built-in mutex writer.
type mutexWriter struct {
w io.Writer
mu sync.Mutex
}

// Write is an implementation of the io.Writer interface.
func (w *mutexWriter) Write(p []byte) (n int, err error) {
w.mu.Lock()
defer w.mu.Unlock()
n, err = w.w.Write(p)
return
}
64 changes: 64 additions & 0 deletions mutex_writer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright 2022 The ZKits Project Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package logger

import (
"bytes"
"sync"
"testing"
)

func TestNewMutexWriter(t *testing.T) {
w := new(bytes.Buffer)
if got := NewMutexWriter(w); got == nil {
t.Fatal("NewMutexWriter(): return nil")
}
}

type testUnsafeWriter struct {
n uint32
m map[uint32][]byte
}

func (w *testUnsafeWriter) Write(p []byte) (int, error) {
w.n++
p2 := make([]byte, len(p))
copy(p2, p)
w.m[w.n] = p2
return len(p2), nil
}

func TestMutexWriter_Write(t *testing.T) {
iw := &testUnsafeWriter{
m: make(map[uint32][]byte),
}
w := NewMutexWriter(iw)
l := New("test").SetOutput(w)
wg := new(sync.WaitGroup)
wg.Add(10)
for i := 0; i < 10; i++ {
go func() {
defer wg.Done()
l.Info("test")
}()
}
wg.Wait()
if len(iw.m) != 10 {
t.Fatalf("MutexWriter: want %d, got %d", 10, len(iw.m))
}
if iw.n != 10 {
t.Fatalf("MutexWriter: want %d, got %d", 10, iw.n)
}
}

0 comments on commit fcac028

Please sign in to comment.