mirror of
https://github.com/octoleo/syncthing.git
synced 2024-11-09 23:00:58 +00:00
acd767b30b
Grab-bag packages are nasty, this cleans it up a little by splitting it into topical packages sempahore, netutil, stringutil, structutil.
62 lines
1.2 KiB
Go
62 lines
1.2 KiB
Go
// 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 testutil
|
|
|
|
import (
|
|
"errors"
|
|
"sync"
|
|
)
|
|
|
|
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(_ []byte) (int, error) {
|
|
<-rw.c
|
|
return 0, ErrClosed
|
|
}
|
|
|
|
func (rw *BlockingRW) Write(_ []byte) (int, error) {
|
|
<-rw.c
|
|
return 0, ErrClosed
|
|
}
|
|
|
|
func (rw *BlockingRW) Close() error {
|
|
rw.closeOnce.Do(func() {
|
|
close(rw.c)
|
|
})
|
|
return nil
|
|
}
|
|
|
|
// NoopRW implements io.Reader and Writer but never returns when called
|
|
type NoopRW struct{}
|
|
|
|
func (*NoopRW) Read(p []byte) (n int, err error) {
|
|
return len(p), nil
|
|
}
|
|
|
|
func (*NoopRW) Write(p []byte) (n int, err error) {
|
|
return len(p), nil
|
|
}
|
|
|
|
type NoopCloser struct{}
|
|
|
|
func (NoopCloser) Close() error {
|
|
return nil
|
|
}
|