2019-05-18 06:53:59 +00:00
|
|
|
// Copyright (C) 2019 The Syncthing Authors.
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
|
|
package testutils
|
|
|
|
|
2020-11-27 10:31:20 +00:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"sync"
|
|
|
|
)
|
2019-05-18 06:53:59 +00:00
|
|
|
|
2020-11-27 10:31:20 +00:00
|
|
|
var ErrClosed = errors.New("closed")
|
|
|
|
|
|
|
|
// BlockingRW implements io.Reader, Writer and Closer, but only returns when closed
|
|
|
|
type BlockingRW struct {
|
|
|
|
c chan struct{}
|
|
|
|
closeOnce sync.Once
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewBlockingRW() *BlockingRW {
|
|
|
|
return &BlockingRW{
|
|
|
|
c: make(chan struct{}),
|
|
|
|
closeOnce: sync.Once{},
|
|
|
|
}
|
|
|
|
}
|
2022-07-28 15:17:29 +00:00
|
|
|
func (rw *BlockingRW) Read(_ []byte) (int, error) {
|
2020-11-27 10:31:20 +00:00
|
|
|
<-rw.c
|
|
|
|
return 0, ErrClosed
|
|
|
|
}
|
|
|
|
|
2022-07-28 15:17:29 +00:00
|
|
|
func (rw *BlockingRW) Write(_ []byte) (int, error) {
|
2020-11-27 10:31:20 +00:00
|
|
|
<-rw.c
|
|
|
|
return 0, ErrClosed
|
2019-05-18 06:53:59 +00:00
|
|
|
}
|
|
|
|
|
2020-11-27 10:31:20 +00:00
|
|
|
func (rw *BlockingRW) Close() error {
|
|
|
|
rw.closeOnce.Do(func() {
|
|
|
|
close(rw.c)
|
|
|
|
})
|
|
|
|
return nil
|
2019-05-18 06:53:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NoopRW implements io.Reader and Writer but never returns when called
|
|
|
|
type NoopRW struct{}
|
|
|
|
|
2022-07-28 15:32:45 +00:00
|
|
|
func (*NoopRW) Read(p []byte) (n int, err error) {
|
2019-05-18 06:53:59 +00:00
|
|
|
return len(p), nil
|
|
|
|
}
|
|
|
|
|
2022-07-28 15:32:45 +00:00
|
|
|
func (*NoopRW) Write(p []byte) (n int, err error) {
|
2019-05-18 06:53:59 +00:00
|
|
|
return len(p), nil
|
|
|
|
}
|
2020-12-21 10:40:51 +00:00
|
|
|
|
|
|
|
type NoopCloser struct{}
|
|
|
|
|
|
|
|
func (NoopCloser) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|