sink_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright (c) 2016 Uber Technologies, Inc.
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE.
  20. package zap
  21. import (
  22. "bytes"
  23. "io"
  24. "net/url"
  25. "strings"
  26. "testing"
  27. "github.com/stretchr/testify/assert"
  28. "github.com/stretchr/testify/require"
  29. "go.uber.org/zap/zapcore"
  30. )
  31. func stubSinkRegistry(t testing.TB) *sinkRegistry {
  32. origSinkRegistry := _sinkRegistry
  33. t.Cleanup(func() {
  34. _sinkRegistry = origSinkRegistry
  35. })
  36. r := newSinkRegistry()
  37. _sinkRegistry = r
  38. return r
  39. }
  40. func TestRegisterSink(t *testing.T) {
  41. stubSinkRegistry(t)
  42. const (
  43. memScheme = "mem"
  44. nopScheme = "no-op.1234"
  45. )
  46. var memCalls, nopCalls int
  47. buf := bytes.NewBuffer(nil)
  48. memFactory := func(u *url.URL) (Sink, error) {
  49. assert.Equal(t, u.Scheme, memScheme, "Scheme didn't match registration.")
  50. memCalls++
  51. return nopCloserSink{zapcore.AddSync(buf)}, nil
  52. }
  53. nopFactory := func(u *url.URL) (Sink, error) {
  54. assert.Equal(t, u.Scheme, nopScheme, "Scheme didn't match registration.")
  55. nopCalls++
  56. return nopCloserSink{zapcore.AddSync(io.Discard)}, nil
  57. }
  58. require.NoError(t, RegisterSink(strings.ToUpper(memScheme), memFactory), "Failed to register scheme %q.", memScheme)
  59. require.NoError(t, RegisterSink(nopScheme, nopFactory), "Failed to register scheme %q.", nopScheme)
  60. sink, closeSink, err := Open(
  61. memScheme+"://somewhere",
  62. nopScheme+"://somewhere-else",
  63. )
  64. require.NoError(t, err, "Unexpected error opening URLs with registered schemes.")
  65. defer closeSink()
  66. assert.Equal(t, 1, memCalls, "Unexpected number of calls to memory factory.")
  67. assert.Equal(t, 1, nopCalls, "Unexpected number of calls to no-op factory.")
  68. _, err = sink.Write([]byte("foo"))
  69. assert.NoError(t, err, "Failed to write to combined WriteSyncer.")
  70. assert.Equal(t, "foo", buf.String(), "Unexpected buffer contents.")
  71. }
  72. func TestRegisterSinkErrors(t *testing.T) {
  73. nopFactory := func(_ *url.URL) (Sink, error) {
  74. return nopCloserSink{zapcore.AddSync(io.Discard)}, nil
  75. }
  76. tests := []struct {
  77. scheme string
  78. err string
  79. }{
  80. {"", "empty string"},
  81. {"FILE", "already registered"},
  82. {"42", "not a valid scheme"},
  83. {"http*", "not a valid scheme"},
  84. }
  85. for _, tt := range tests {
  86. t.Run("scheme-"+tt.scheme, func(t *testing.T) {
  87. r := newSinkRegistry()
  88. err := r.RegisterSink(tt.scheme, nopFactory)
  89. assert.ErrorContains(t, err, tt.err)
  90. })
  91. }
  92. }