github.com/go-spring/spring-base@v1.1.3/log/plugin_appender_test.go (about)

     1  /*
     2   * Copyright 2012-2019 the original author or authors.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *      https://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package log_test
    18  
    19  import (
    20  	"context"
    21  	"sync"
    22  	"testing"
    23  
    24  	"github.com/go-spring/spring-base/assert"
    25  	"github.com/go-spring/spring-base/atomic"
    26  	"github.com/go-spring/spring-base/log"
    27  )
    28  
    29  type CountingNoOpAppender struct {
    30  	log.BaseAppender
    31  	count atomic.Int64
    32  }
    33  
    34  func (c *CountingNoOpAppender) Count() int64        { return c.count.Load() }
    35  func (c *CountingNoOpAppender) Append(e *log.Event) { c.count.Add(1) }
    36  
    37  func TestCountingNoOpAppender(t *testing.T) {
    38  	appender := &CountingNoOpAppender{}
    39  	var wg sync.WaitGroup
    40  	wg.Add(2)
    41  	go func() {
    42  		defer wg.Done()
    43  		for i := 0; i < 3; i++ {
    44  			appender.Append(nil)
    45  		}
    46  	}()
    47  	go func() {
    48  		defer wg.Done()
    49  		for i := 0; i < 6; i++ {
    50  			appender.Append(nil)
    51  		}
    52  	}()
    53  	wg.Wait()
    54  	assert.Equal(t, appender.Count(), int64(9))
    55  }
    56  
    57  func TestNullAppender(t *testing.T) {
    58  	appender := new(log.NullAppender)
    59  	err := appender.Start()
    60  	assert.Nil(t, err)
    61  	appender.Stop(context.Background())
    62  	name := appender.GetName()
    63  	assert.Equal(t, name, "")
    64  	layout := appender.GetLayout()
    65  	assert.Nil(t, layout)
    66  	appender.Append(nil)
    67  }
    68  
    69  func TestConsoleAppender(t *testing.T) {
    70  	//appender := &log.ConsoleAppender{
    71  	//	BaseAppender: log.BaseAppender{
    72  	//		Layout: nil,
    73  	//	},
    74  	//}
    75  }