...

Source file src/github.com/dsoprea/go-logging/log_test.go

Documentation: github.com/dsoprea/go-logging

     1  package log
     2  
     3  import (
     4  	e "errors"
     5  	"testing"
     6  
     7  	"math/rand"
     8  )
     9  
    10  // Extends the default environment configuration-provider to set the level to
    11  // "debug" (so none of our messages get filtered).
    12  type testConfigurationProvider struct {
    13  	levelName string
    14  }
    15  
    16  func newTestConfigurationProvider(levelName string) ConfigurationProvider {
    17  	if levelName == "" {
    18  		levelName = LevelNameError
    19  	}
    20  
    21  	return &testConfigurationProvider{
    22  		levelName: levelName,
    23  	}
    24  }
    25  
    26  func (ec *testConfigurationProvider) Format() string {
    27  	return ""
    28  }
    29  
    30  func (ec *testConfigurationProvider) DefaultAdapterName() string {
    31  	return ""
    32  }
    33  
    34  func (ec *testConfigurationProvider) LevelName() string {
    35  	return ec.levelName
    36  }
    37  
    38  func (ec *testConfigurationProvider) IncludeNouns() string {
    39  	return ""
    40  }
    41  
    42  func (ec *testConfigurationProvider) ExcludeNouns() string {
    43  	return ""
    44  }
    45  
    46  func (ec *testConfigurationProvider) ExcludeBypassLevelName() string {
    47  	return ""
    48  }
    49  
    50  /*
    51  type testConfigurationProvider struct {
    52      EnvironmentConfigurationProvider
    53  }
    54  
    55  func (tec *testConfigurationProvider) LevelName() string {
    56      return LevelNameDebug
    57  }
    58  */
    59  
    60  // A test logging-adapter that sets flags as certain messages are received.
    61  type testLogAdapter struct {
    62  	id int
    63  
    64  	debugTriggered   bool
    65  	infoTriggered    bool
    66  	warningTriggered bool
    67  	errorTriggered   bool
    68  }
    69  
    70  func newTestLogAdapter() LogAdapter {
    71  	return &testLogAdapter{
    72  		id: rand.Int(),
    73  	}
    74  }
    75  
    76  func (tla *testLogAdapter) Debugf(lc *LogContext, message *string) error {
    77  	tla.debugTriggered = true
    78  
    79  	return nil
    80  }
    81  
    82  func (tla *testLogAdapter) Infof(lc *LogContext, message *string) error {
    83  	tla.infoTriggered = true
    84  
    85  	return nil
    86  }
    87  
    88  func (tla *testLogAdapter) Warningf(lc *LogContext, message *string) error {
    89  	tla.warningTriggered = true
    90  
    91  	return nil
    92  }
    93  
    94  func (tla *testLogAdapter) Errorf(lc *LogContext, message *string) error {
    95  	tla.errorTriggered = true
    96  
    97  	return nil
    98  }
    99  
   100  // Tests
   101  
   102  func TestConfigurationOverride(t *testing.T) {
   103  	cs := getConfigState()
   104  	defer func() {
   105  		setConfigState(cs)
   106  	}()
   107  
   108  	levelName = "xyz"
   109  
   110  	// Overwrite configuration, first thing.
   111  	tcp := newTestConfigurationProvider(LevelNameDebug)
   112  	LoadConfiguration(tcp)
   113  
   114  	if levelName != LevelNameDebug {
   115  		t.Error("The test configuration-provider didn't override the level properly.", levelName)
   116  	}
   117  }
   118  
   119  func TestConfigurationLevelDirectOverride(t *testing.T) {
   120  	// Overwrite configuration, first thing.
   121  	tcp := newTestConfigurationProvider("")
   122  	LoadConfiguration(tcp)
   123  
   124  	ClearAdapters()
   125  
   126  	tla1 := newTestLogAdapter()
   127  	AddAdapter("test", tla1)
   128  
   129  	l := NewLoggerWithAdapterName("logTest", "test")
   130  
   131  	// Usually we don't configure until the first message. Force it.
   132  	l.doConfigure(false)
   133  	tla2 := l.Adapter().(*testLogAdapter)
   134  
   135  	if tla2.debugTriggered != false {
   136  		t.Error("Debug flag should've been FALSE initially but wasn't.")
   137  	}
   138  
   139  	// Set the level high to prevent logging, first.
   140  	levelName = LevelNameError
   141  
   142  	// Force a reconfig (which will bring in the new level).
   143  	l.doConfigure(true)
   144  
   145  	// Re-retrieve. This is reconstructed during reconfiguration.
   146  	tla3 := l.Adapter().(*testLogAdapter)
   147  
   148  	l.Debugf(nil, "Debug message")
   149  
   150  	if tla3.debugTriggered != false {
   151  		t.Error("Debug message not through but wasn't supposed to.")
   152  	}
   153  
   154  	// Now, set the level low to allow logging.
   155  	levelName = LevelNameDebug
   156  
   157  	// Force a reconfig (which will bring in the new level).
   158  	l.doConfigure(true)
   159  
   160  	// Re-retrieve. This is reconstructed during reconfiguration.
   161  	tla4 := l.Adapter().(*testLogAdapter)
   162  
   163  	l.Debugf(nil, "Debug message")
   164  
   165  	if tla4.debugTriggered == false {
   166  		t.Error("Debug message not getting through.")
   167  	}
   168  }
   169  
   170  func TestConfigurationLevelProviderOverride(t *testing.T) {
   171  	cs := getConfigState()
   172  	defer func() {
   173  		setConfigState(cs)
   174  	}()
   175  
   176  	// Overwrite configuration, first thing.
   177  	tcp := newTestConfigurationProvider("")
   178  	LoadConfiguration(tcp)
   179  
   180  	ClearAdapters()
   181  
   182  	tla1 := newTestLogAdapter()
   183  	AddAdapter("test", tla1)
   184  
   185  	l := NewLoggerWithAdapterName("logTest", "test")
   186  
   187  	// Usually we don't configure until the first message. Force it.
   188  	l.doConfigure(false)
   189  	tla2 := l.Adapter().(*testLogAdapter)
   190  
   191  	if tla2.debugTriggered != false {
   192  		t.Error("Debug flag should've been FALSE initially but wasn't.")
   193  	}
   194  
   195  	// Set the level high to prevent logging, first.
   196  	tcp = newTestConfigurationProvider(LevelNameError)
   197  	LoadConfiguration(tcp)
   198  
   199  	// Force a reconfig (which will bring in the new level).
   200  	l.doConfigure(true)
   201  
   202  	// Re-retrieve. This is reconstructed during reconfiguration.
   203  	tla3 := l.Adapter().(*testLogAdapter)
   204  
   205  	l.Debugf(nil, "Debug message")
   206  
   207  	if tla3.debugTriggered != false {
   208  		t.Error("Debug message not through but wasn't supposed to.")
   209  	}
   210  
   211  	// Now, set the level low to allow logging.
   212  	tcp = newTestConfigurationProvider(LevelNameDebug)
   213  	LoadConfiguration(tcp)
   214  
   215  	// Force a reconfig (which will bring in the new level).
   216  	l.doConfigure(true)
   217  
   218  	// Re-retrieve. This is reconstructed during reconfiguration.
   219  	tla4 := l.Adapter().(*testLogAdapter)
   220  
   221  	l.Debugf(nil, "Debug message")
   222  
   223  	if tla4.debugTriggered == false {
   224  		t.Error("Debug message not getting through.")
   225  	}
   226  }
   227  
   228  func TestDefaultAdapterAssignment(t *testing.T) {
   229  	SetDefaultAdapterName("")
   230  
   231  	ClearAdapters()
   232  
   233  	tla := newTestLogAdapter()
   234  	AddAdapter("test1", tla)
   235  
   236  	an := GetDefaultAdapterName()
   237  	if an == "" {
   238  		t.Error("Default adapter not set after registration.")
   239  	}
   240  
   241  	an = GetDefaultAdapterName()
   242  	if an != "test1" {
   243  		t.Error("Default adapter not set to our adapter after registration.", an)
   244  	}
   245  
   246  	SetDefaultAdapterName("test2")
   247  	an = GetDefaultAdapterName()
   248  	if an != "test2" {
   249  		t.Error("SetDefaultAdapterName() did not set default adapter correctly.", an)
   250  	}
   251  }
   252  
   253  func TestAdapter(t *testing.T) {
   254  	cs := getConfigState()
   255  	defer func() {
   256  		setConfigState(cs)
   257  	}()
   258  
   259  	// Overwrite configuration, first thing.
   260  	tcp := newTestConfigurationProvider(LevelNameDebug)
   261  	LoadConfiguration(tcp)
   262  
   263  	ClearAdapters()
   264  
   265  	tla1 := newTestLogAdapter()
   266  	AddAdapter("test", tla1)
   267  
   268  	l := NewLoggerWithAdapterName("logTest", "test")
   269  
   270  	l.doConfigure(false)
   271  
   272  	tla2 := l.Adapter().(*testLogAdapter)
   273  
   274  	l.Debugf(nil, "Debug message")
   275  	if tla2.debugTriggered == false {
   276  		t.Error("Debug message not getting through.")
   277  	}
   278  
   279  	l.Infof(nil, "Info message")
   280  	if tla2.infoTriggered == false {
   281  		t.Error("Info message not getting through.")
   282  	}
   283  
   284  	l.Warningf(nil, "Warning message")
   285  	if tla2.warningTriggered == false {
   286  		t.Error("Warning message not getting through.")
   287  	}
   288  
   289  	err := e.New("an error happened")
   290  	l.Errorf(nil, err, "Error message")
   291  	if tla2.errorTriggered == false {
   292  		t.Error("Error message not getting through.")
   293  	}
   294  }
   295  
   296  func TestStaticConfiguration(t *testing.T) {
   297  	scp := NewStaticConfigurationProvider()
   298  
   299  	cs := getConfigState()
   300  	defer func() {
   301  		setConfigState(cs)
   302  	}()
   303  
   304  	scp.SetFormat("aa")
   305  	scp.SetDefaultAdapterName("bb")
   306  	scp.SetLevelName("cc")
   307  	scp.SetIncludeNouns("dd")
   308  	scp.SetExcludeNouns("ee")
   309  	scp.SetExcludeBypassLevelName("ff")
   310  
   311  	LoadConfiguration(scp)
   312  
   313  	if format != "aa" {
   314  		t.Error("Static configuration provider was not set correctly: format")
   315  	}
   316  
   317  	if defaultAdapterName != "bb" {
   318  		t.Error("Static configuration provider was not set correctly: defaultAdapterName")
   319  	}
   320  
   321  	if levelName != "cc" {
   322  		t.Error("Static configuration provider was not set correctly: levelName")
   323  	}
   324  
   325  	if includeNouns != "dd" {
   326  		t.Error("Static configuration provider was not set correctly: includeNouns")
   327  	}
   328  
   329  	if excludeNouns != "ee" {
   330  		t.Error("Static configuration provider was not set correctly: excludeNouns")
   331  	}
   332  
   333  	if excludeBypassLevelName != "ff" {
   334  		t.Error("Static configuration provider was not set correctly: excludeBypassLevelName")
   335  	}
   336  }
   337  
   338  func TestNoAdapter(t *testing.T) {
   339  	ClearAdapters()
   340  
   341  	l := NewLogger("logTest")
   342  
   343  	if l.Adapter() != nil {
   344  		t.Error("Logger has an adapter at init when no adapters were available.")
   345  	}
   346  
   347  	l.doConfigure(false)
   348  
   349  	if l.Adapter() != nil {
   350  		t.Error("Logger has an adapter after configuration no adapters were available.")
   351  	}
   352  
   353  	// Should execute, but nothing will happen.
   354  	err := e.New("an error happened")
   355  	l.Errorf(nil, err, "Error message")
   356  }
   357  
   358  func TestNewLogger(t *testing.T) {
   359  	noun := "logTest"
   360  
   361  	l := NewLogger(noun)
   362  	if l.noun != noun {
   363  		t.Fatalf("Noun not correct: [%s]", l.noun)
   364  	}
   365  }
   366  
   367  func TestNewLoggerWithAdapterName(t *testing.T) {
   368  	noun := "logTest"
   369  
   370  	originalDefaultAdapterName := GetDefaultAdapterName()
   371  
   372  	adapterName := "abcdef"
   373  
   374  	cla := NewConsoleLogAdapter()
   375  	AddAdapter(adapterName, cla)
   376  
   377  	SetDefaultAdapterName(adapterName)
   378  
   379  	defer func() {
   380  		SetDefaultAdapterName(originalDefaultAdapterName)
   381  		delete(adapters, adapterName)
   382  	}()
   383  
   384  	l := NewLoggerWithAdapterName(noun, adapterName)
   385  	if l.noun != noun {
   386  		t.Fatalf("Noun not correct: [%s]", l.noun)
   387  	} else if l.an != adapterName {
   388  		t.Fatalf("Adapter-name not correct: [%s]", l.an)
   389  	}
   390  }
   391  
   392  func TestIs__unwrapped__hit(t *testing.T) {
   393  	e1 := e.New("test error")
   394  	if Is(e1, e1) != true {
   395  		t.Fatalf("Is() should be true for an unwrapped success")
   396  	}
   397  }
   398  
   399  func TestIs__unwrapped__miss(t *testing.T) {
   400  	e1 := e.New("test error")
   401  	e2 := e.New("test error 2")
   402  
   403  	if Is(e1, e2) != false {
   404  		t.Fatalf("Is() should be false for an unwrapped failure")
   405  	}
   406  }
   407  
   408  func TestIs__wrapped__hit(t *testing.T) {
   409  	e2 := e.New("test error")
   410  	e1 := Wrap(e2)
   411  	if Is(e1, e2) != true {
   412  		t.Fatalf("Is() should be true for a wrapped success")
   413  	}
   414  }
   415  
   416  func TestIs__wrapped__miss(t *testing.T) {
   417  	e1 := Errorf("test error")
   418  	e2 := e.New("test error 2")
   419  
   420  	if Is(e1, e2) != false {
   421  		t.Fatalf("Is() should be false for a wrapped failure")
   422  	}
   423  }
   424  

View as plain text