sink_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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/ioutil"
  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 TestRegisterSink(t *testing.T) {
  32. const (
  33. memScheme = "m"
  34. nopScheme = "no-op.1234"
  35. )
  36. var memCalls, nopCalls int
  37. buf := bytes.NewBuffer(nil)
  38. memFactory := func(u *url.URL) (Sink, error) {
  39. assert.Equal(t, u.Scheme, memScheme, "Scheme didn't match registration.")
  40. memCalls++
  41. return nopCloserSink{zapcore.AddSync(buf)}, nil
  42. }
  43. nopFactory := func(u *url.URL) (Sink, error) {
  44. assert.Equal(t, u.Scheme, nopScheme, "Scheme didn't match registration.")
  45. nopCalls++
  46. return nopCloserSink{zapcore.AddSync(ioutil.Discard)}, nil
  47. }
  48. defer resetSinkRegistry()
  49. require.NoError(t, RegisterSink(strings.ToUpper(memScheme), memFactory), "Failed to register scheme %q.", memScheme)
  50. require.NoError(t, RegisterSink(nopScheme, nopFactory), "Failed to register scheme %q.", memScheme)
  51. sink, close, err := Open(
  52. memScheme+"://somewhere",
  53. nopScheme+"://somewhere-else",
  54. )
  55. assert.NoError(t, err, "Unexpected error opening URLs with registered schemes.")
  56. defer close()
  57. assert.Equal(t, 1, memCalls, "Unexpected number of calls to memory factory.")
  58. assert.Equal(t, 1, nopCalls, "Unexpected number of calls to no-op factory.")
  59. _, err = sink.Write([]byte("foo"))
  60. assert.NoError(t, err, "Failed to write to combined WriteSyncer.")
  61. assert.Equal(t, "foo", buf.String(), "Unexpected buffer contents.")
  62. }
  63. func TestRegisterSinkErrors(t *testing.T) {
  64. nopFactory := func(_ *url.URL) (Sink, error) {
  65. return nopCloserSink{zapcore.AddSync(ioutil.Discard)}, nil
  66. }
  67. tests := []struct {
  68. scheme string
  69. err string
  70. }{
  71. {"", "empty string"},
  72. {"FILE", "already registered"},
  73. {"42", "not a valid scheme"},
  74. {"http*", "not a valid scheme"},
  75. }
  76. for _, tt := range tests {
  77. t.Run("scheme-"+tt.scheme, func(t *testing.T) {
  78. defer resetSinkRegistry()
  79. err := RegisterSink(tt.scheme, nopFactory)
  80. if assert.Error(t, err, "expected error") {
  81. assert.Contains(t, err.Error(), tt.err, "unexpected error")
  82. }
  83. })
  84. }
  85. }