-
Notifications
You must be signed in to change notification settings - Fork 5
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
1 parent
e62abc7
commit bf4b3de
Showing
4 changed files
with
95 additions
and
62 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
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,39 @@ | ||
package port | ||
|
||
import ( | ||
"github.com/siyul-park/uniflow/pkg/packet" | ||
"github.com/siyul-park/uniflow/pkg/process" | ||
) | ||
|
||
// Pipe is a connection between two ports. | ||
func Pipe() (*InPort, *OutPort) { | ||
inPort, outPort := NewIn(), NewOut() | ||
|
||
inPort.AddListener(ListenFunc(func(proc *process.Process) { | ||
reader := inPort.Open(proc) | ||
var writer *packet.Writer | ||
|
||
for inPck := range reader.Read() { | ||
if writer == nil { | ||
writer = outPort.Open(proc) | ||
} | ||
if writer.Write(inPck) == 0 { | ||
reader.Receive(inPck) | ||
} | ||
} | ||
})) | ||
|
||
outPort.AddListener(ListenFunc(func(proc *process.Process) { | ||
var reader *packet.Reader | ||
writer := outPort.Open(proc) | ||
|
||
for backPck := range writer.Receive() { | ||
if reader == nil { | ||
reader = inPort.Open(proc) | ||
} | ||
reader.Receive(backPck) | ||
} | ||
})) | ||
|
||
return inPort, outPort | ||
} |
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,45 @@ | ||
package port | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/go-faker/faker/v4" | ||
"github.com/siyul-park/uniflow/pkg/packet" | ||
"github.com/siyul-park/uniflow/pkg/process" | ||
"github.com/siyul-park/uniflow/pkg/types" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestPipe(t *testing.T) { | ||
inPort0, outPort0 := Pipe() | ||
defer inPort0.Close() | ||
defer outPort0.Close() | ||
|
||
inPort1, outPort1 := NewIn(), NewOut() | ||
defer inPort1.Close() | ||
defer outPort1.Close() | ||
|
||
outPort0.Link(inPort1) | ||
outPort1.Link(inPort0) | ||
|
||
proc := process.New() | ||
defer proc.Exit(nil) | ||
|
||
inWriter := outPort1.Open(proc) | ||
outReader := inPort1.Open(proc) | ||
|
||
ctx, cancel := context.WithTimeout(context.TODO(), time.Second) | ||
defer cancel() | ||
|
||
inPck := packet.New(types.NewString(faker.UUIDHyphenated())) | ||
inWriter.Write(inPck) | ||
|
||
select { | ||
case outPck := <-outReader.Read(): | ||
assert.Equal(t, inPck.Payload(), outPck.Payload()) | ||
case <-ctx.Done(): | ||
assert.NoError(t, ctx.Err()) | ||
} | ||
} |
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