-
Notifications
You must be signed in to change notification settings - Fork 339
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
214 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Copyright 2021 The Swarm Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
// Package flipflop exposes a buffered input functionality | ||
// that mimicks the behavior of falling edge detection | ||
// which is done when doing signal processing on digital | ||
// or analog electric circuitry. | ||
package flipflop |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright 2021 The Swarm Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package flipflop | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
type detector struct { | ||
t time.Duration | ||
worstCase time.Duration | ||
|
||
buf chan struct{} | ||
out chan struct{} | ||
quit chan struct{} | ||
} | ||
|
||
// NewFallingEdge returns a new falling edge detector. | ||
// bufferTime is the time to buffer, worstCase is buffertime*worstcase time to wait before writing | ||
// to the output anyway. | ||
func NewFallingEdge(bufferTime, worstCase time.Duration) (in chan<- struct{}, out <-chan struct{}, clean func()) { | ||
d := &detector{ | ||
t: bufferTime, | ||
worstCase: worstCase, | ||
buf: make(chan struct{}, 1), | ||
out: make(chan struct{}), | ||
quit: make(chan struct{}), | ||
} | ||
|
||
go d.work() | ||
|
||
return d.buf, d.out, func() { close(d.quit) } | ||
} | ||
|
||
func (d *detector) work() { | ||
var waitWrite <-chan time.Time | ||
var worstCase <-chan time.Time | ||
for { | ||
select { | ||
case <-d.quit: | ||
return | ||
case <-d.buf: | ||
// we have an item in the buffer, dont announce yet | ||
waitWrite = time.After(d.t) | ||
if worstCase == nil { | ||
worstCase = time.After(d.worstCase) | ||
} | ||
case <-waitWrite: | ||
select { | ||
case d.out <- struct{}{}: | ||
case <-d.quit: | ||
return | ||
} | ||
worstCase = nil | ||
waitWrite = nil | ||
case <-worstCase: | ||
select { | ||
case d.out <- struct{}{}: | ||
case <-d.quit: | ||
return | ||
} | ||
worstCase = nil | ||
waitWrite = nil | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
// Copyright 2021 The Swarm Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package flipflop_test | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/ethersphere/bee/pkg/flipflop" | ||
) | ||
|
||
func TestFallingEdge(t *testing.T) { | ||
t.Parallel() | ||
t.Skip("github actions") | ||
|
||
ok := make(chan struct{}) | ||
tt := 50 * time.Millisecond | ||
worst := 5 * tt | ||
in, c, cleanup := flipflop.NewFallingEdge(tt, worst) | ||
defer cleanup() | ||
go func() { | ||
select { | ||
case <-c: | ||
close(ok) | ||
return | ||
case <-time.After(100 * time.Millisecond): | ||
t.Error("timed out") | ||
} | ||
}() | ||
|
||
in <- struct{}{} | ||
|
||
select { | ||
case <-ok: | ||
case <-time.After(1 * time.Second): | ||
t.Fatal("timed out") | ||
} | ||
} | ||
|
||
func TestFallingEdgeBuffer(t *testing.T) { | ||
t.Parallel() | ||
t.Skip("needs parameter tweaking on github actions") | ||
|
||
ok := make(chan struct{}) | ||
tt := 150 * time.Millisecond | ||
worst := 9 * tt | ||
in, c, cleanup := flipflop.NewFallingEdge(tt, worst) | ||
defer cleanup() | ||
sleeps := 5 | ||
wait := 50 * time.Millisecond | ||
|
||
start := time.Now() | ||
online := make(chan struct{}) | ||
go func() { | ||
close(online) | ||
select { | ||
case <-c: | ||
if time.Since(start) <= 450*time.Millisecond { | ||
t.Errorf("wrote too early %v", time.Since(start)) | ||
} | ||
close(ok) | ||
return | ||
case <-time.After(1000 * time.Millisecond): | ||
t.Error("timed out") | ||
} | ||
}() | ||
|
||
// wait for goroutine to be scheduled | ||
<-online | ||
|
||
for i := 0; i < sleeps; i++ { | ||
in <- struct{}{} | ||
time.Sleep(wait) | ||
} | ||
select { | ||
case <-ok: | ||
case <-time.After(1 * time.Second): | ||
t.Fatal("timed out") | ||
} | ||
} | ||
|
||
func TestFallingEdgeWorstCase(t *testing.T) { | ||
t.Parallel() | ||
t.Skip("github actions") | ||
|
||
ok := make(chan struct{}) | ||
tt := 100 * time.Millisecond | ||
worst := 5 * tt | ||
in, c, cleanup := flipflop.NewFallingEdge(tt, worst) | ||
defer cleanup() | ||
sleeps := 9 | ||
wait := 80 * time.Millisecond | ||
|
||
start := time.Now() | ||
|
||
go func() { | ||
select { | ||
case <-c: | ||
if time.Since(start) >= 550*time.Millisecond { | ||
t.Errorf("wrote too early %v", time.Since(start)) | ||
} | ||
|
||
close(ok) | ||
return | ||
case <-time.After(1000 * time.Millisecond): | ||
t.Error("timed out") | ||
} | ||
}() | ||
go func() { | ||
for i := 0; i < sleeps; i++ { | ||
in <- struct{}{} | ||
time.Sleep(wait) | ||
} | ||
}() | ||
select { | ||
case <-ok: | ||
case <-time.After(1 * time.Second): | ||
t.Fatal("timed out") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters