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{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func (rw *BlockingRW) Read(p []byte) (int, error) {
|
|
|
|
<-rw.c
|
|
|
|
return 0, ErrClosed
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rw *BlockingRW) Write(p []byte) (int, error) {
|
|
|
|
<-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{}
|
|
|
|
|
|
|
|
func (rw *NoopRW) Read(p []byte) (n int, err error) {
|
|
|
|
return len(p), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rw *NoopRW) Write(p []byte) (n int, err error) {
|
|
|
|
return len(p), nil
|
|
|
|
}
|