| 1 | // Copyright (c) The EfficientGo Authors. |
| 2 | // Licensed under the Apache License 2.0. |
| 3 | |
| 4 | // Initially copied from Thanos |
| 5 | // |
| 6 | // Copyright (c) The Thanos Authors. |
| 7 | // Licensed under the Apache License 2.0. |
| 8 | |
| 9 | package logerrcapturescip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/logerrcapture`/ |
| 10 | |
| 11 | import ( |
| 12 | "ioscip-go gomod github.com/golang/go/src go1.17 io/" |
| 13 | "osscip-go gomod github.com/golang/go/src go1.17 os/" |
| 14 | "stringsscip-go gomod github.com/golang/go/src go1.17 strings/" |
| 15 | "testingscip-go gomod github.com/golang/go/src go1.17 testing/" |
| 16 | |
| 17 | "github.com/efficientgo/core/errorsscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/" |
| 18 | "github.com/efficientgo/core/testutilscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/testutil`/" |
| 19 | ) |
| 20 | |
| 21 | type loggerCapturerscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/logerrcapture`/loggerCapturer# struct { |
| 22 | // WasCalled is true if the Log() function has been called. |
| 23 | WasCalledscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/logerrcapture`/loggerCapturer#WasCalled. bool |
| 24 | } |
| 25 | |
| 26 | func (lc *loggerCapturerscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/logerrcapture`/loggerCapturer#) Logscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/logerrcapture`/loggerCapturer#Log().(keyvals ...interface{}) error { |
| 27 | lc.WasCalledscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/logerrcapture`/loggerCapturer#WasCalled. = true |
| 28 | return nil |
| 29 | } |
| 30 | |
| 31 | type emulatedCloserscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/logerrcapture`/emulatedCloser# struct { |
| 32 | ioscip-go gomod github.com/golang/go/src go1.17 io/.Readerscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/logerrcapture`/emulatedCloser#Reader. |
| 33 | |
| 34 | callsscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/logerrcapture`/emulatedCloser#calls. int |
| 35 | } |
| 36 | |
| 37 | func (e *emulatedCloserscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/logerrcapture`/emulatedCloser#) Closescip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/logerrcapture`/emulatedCloser#Close().() error { |
| 38 | e.callsscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/logerrcapture`/emulatedCloser#calls.++ |
| 39 | if e.callsscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/logerrcapture`/emulatedCloser#calls. == 1 { |
| 40 | return nil |
| 41 | } |
| 42 | if e.callsscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/logerrcapture`/emulatedCloser#calls. == 2 { |
| 43 | return errorsscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/.Wrapscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/Wrap().(osscip-go gomod github.com/golang/go/src go1.17 os/.ErrClosedscip-go gomod github.com/golang/go/src go1.17 os/ErrClosed., "can even be a wrapped one") |
| 44 | } |
| 45 | return errorsscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/.Newscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/New().("something very bad happened") |
| 46 | } |
| 47 | |
| 48 | // newEmulatedCloser returns a ReadCloser with a Close method |
| 49 | // that at first returns success but then returns that |
| 50 | // it has been closed already. After that, it returns that |
| 51 | // something very bad had happened. |
| 52 | func newEmulatedCloserscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/logerrcapture`/newEmulatedCloser().(r ioscip-go gomod github.com/golang/go/src go1.17 io/.Readerscip-go gomod github.com/golang/go/src go1.17 io/Reader#) ioscip-go gomod github.com/golang/go/src go1.17 io/.ReadCloserscip-go gomod github.com/golang/go/src go1.17 io/ReadCloser# { |
| 53 | return &emulatedCloserscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/logerrcapture`/emulatedCloser#{Readerscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/logerrcapture`/emulatedCloser#Reader.: r} |
| 54 | } |
| 55 | |
| 56 | func TestCloseMoreThanOncescip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/logerrcapture`/TestCloseMoreThanOnce().(t *testingscip-go gomod github.com/golang/go/src go1.17 testing/.Tscip-go gomod github.com/golang/go/src go1.17 testing/T#) { |
| 57 | lc := &loggerCapturerscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/logerrcapture`/loggerCapturer#{} |
| 58 | r := newEmulatedCloserscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/logerrcapture`/newEmulatedCloser().(stringsscip-go gomod github.com/golang/go/src go1.17 strings/.NewReaderscip-go gomod github.com/golang/go/src go1.17 strings/NewReader().("somestring")) |
| 59 | |
| 60 | Doscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/logerrcapture`/Do().(lc, r.Closescip-go gomod github.com/golang/go/src go1.17 io/Closer#Close()., "should not be called") |
| 61 | Doscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/logerrcapture`/Do().(lc, r.Closescip-go gomod github.com/golang/go/src go1.17 io/Closer#Close()., "should not be called") |
| 62 | testutilscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/testutil`/.Equalsscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/testutil`/Equals().(t, false, lc.WasCalledscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/logerrcapture`/loggerCapturer#WasCalled.) |
| 63 | |
| 64 | Doscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/logerrcapture`/Do().(lc, r.Closescip-go gomod github.com/golang/go/src go1.17 io/Closer#Close()., "should be called") |
| 65 | testutilscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/testutil`/.Equalsscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/testutil`/Equals().(t, true, lc.WasCalledscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/logerrcapture`/loggerCapturer#WasCalled.) |
| 66 | } |