github.com/keakon/golog@v0.0.0-20230330091222-cac71197c18d/handler_test.go (about)

     1  package golog
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func TestHandle(t *testing.T) {
     8  	h := NewHandler(InfoLevel, DefaultFormatter)
     9  	r := &Record{tm: now()}
    10  	if h.Handle(r) {
    11  		t.Error("info handler handled debug record")
    12  	}
    13  
    14  	r.level = InfoLevel
    15  	if !h.Handle(r) {
    16  		t.Error("info handler ignored info record")
    17  	}
    18  
    19  	r.level = ErrorLevel
    20  	if !h.Handle(r) {
    21  		t.Error("error handler ignored info record")
    22  	}
    23  }
    24  
    25  func TestCloseHandler(t *testing.T) {
    26  	h := NewHandler(InfoLevel, DefaultFormatter)
    27  	h.Close()
    28  	h.Close()
    29  
    30  	w := NewDiscardWriter()
    31  	h.AddWriter(w)
    32  	h.Close()
    33  	if len(h.writers) > 0 {
    34  		t.Error("closed handler is not empty")
    35  	}
    36  	if w.Writer != nil {
    37  		t.Error("close handler left its writer opened")
    38  	}
    39  	h.Close()
    40  }