Skip to content

Commit

Permalink
test: test whether the core is replaced in already created loggers
Browse files Browse the repository at this point in the history
  • Loading branch information
MDobak committed Jun 3, 2021
1 parent 60a3410 commit 109a95f
Showing 1 changed file with 22 additions and 20 deletions.
42 changes: 22 additions & 20 deletions setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"os"
"strings"
"testing"

"go.uber.org/zap"
)

func TestGetLoggerDefault(t *testing.T) {
Expand Down Expand Up @@ -206,35 +204,39 @@ func TestSubsystemLevels(t *testing.T) {
}

func TestCustomCore(t *testing.T) {
r, w, err := os.Pipe()
r1, w1, err := os.Pipe()
if err != nil {
t.Fatalf("failed to open pipe: %v", err)
}

stderr := os.Stderr
os.Stderr = w
defer func() {
os.Stderr = stderr
}()

ws, _, err := zap.Open("stdout", "stderr")
r2, w2, err := os.Pipe()
if err != nil {
t.Fatalf("unable to open logging output: %v", err)
t.Fatalf("failed to open pipe: %v", err)
}

// logging should work with the custom core
SetPrimaryCore(newCore(PlaintextOutput, ws, LevelDebug))
SetPrimaryCore(newCore(PlaintextOutput, w1, LevelDebug))
log := getLogger("test")

log.Error("scooby")
w.Close()

buf := &bytes.Buffer{}
if _, err := io.Copy(buf, r); err != nil && err != io.ErrClosedPipe {
// SetPrimaryCore should replace the core in previously created loggers
SetPrimaryCore(newCore(PlaintextOutput, w2, LevelDebug))
log.Error("doo")

w1.Close()
w2.Close()

buf1 := &bytes.Buffer{}
buf2 := &bytes.Buffer{}
if _, err := io.Copy(buf1, r1); err != nil && err != io.ErrClosedPipe {
t.Fatalf("unexpected error: %v", err)
}

if !strings.Contains(buf.String(), "scooby") {
t.Errorf("got %q, wanted it to contain log output", buf.String())
if _, err := io.Copy(buf2, r2); err != nil && err != io.ErrClosedPipe {
t.Fatalf("unexpected error: %v", err)
}
if !strings.Contains(buf1.String(), "scooby") {
t.Errorf("got %q, wanted it to contain log output", buf1.String())
}
if !strings.Contains(buf2.String(), "doo") {
t.Errorf("got %q, wanted it to contain log output", buf2.String())
}
}

0 comments on commit 109a95f

Please sign in to comment.