Skip to content

Commit

Permalink
add Name method to ReceiveChannel and SendChannel interface (#1538)
Browse files Browse the repository at this point in the history
add Name method
  • Loading branch information
bentveljanzx authored Jul 9, 2024
1 parent 79cd73a commit 25f450f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
25 changes: 25 additions & 0 deletions internal/internal_coroutines_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,31 @@ func TestBufferedChannelGet(t *testing.T) {
require.EqualValues(t, expected, history)
}

func TestChannelName(t *testing.T) {
d := createNewDispatcher(func(ctx Context) {
const namedBufferedChannel = "named-buffered-channel"
ch1 := NewNamedBufferedChannel(ctx, namedBufferedChannel, 1)
assert.Equal(t, namedBufferedChannel, ch1.Name())

const namedChannel = "named-channel"
ch2 := NewNamedChannel(ctx, namedChannel)
assert.Equal(t, namedChannel, ch2.Name())

var receiveChannel ReceiveChannel = ch2
assert.Equal(t, namedChannel, receiveChannel.Name())

var sendChannel SendChannel = ch2
assert.Equal(t, namedChannel, sendChannel.Name())

const signalChannel = "signal-channel"
ch3 := GetSignalChannel(ctx, signalChannel)
assert.Equal(t, signalChannel, ch3.Name())
})
defer d.Close()
requireNoExecuteErr(t, d.ExecuteUntilAllBlocked(defaultDeadlockDetectionTimeout))
require.True(t, d.IsDone())
}

func TestNotBlockingSelect(t *testing.T) {
var history []string
d := createNewDispatcher(func(ctx Context) {
Expand Down
4 changes: 4 additions & 0 deletions internal/internal_workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,10 @@ func getStateIfRunning(ctx Context) *coroutineState {
return state
}

func (c *channelImpl) Name() string {
return c.name
}

func (c *channelImpl) CanReceiveWithoutBlocking() bool {
return c.recValue != nil || len(c.buffer) > 0 || len(c.blockedSends) > 0 || c.closed
}
Expand Down
14 changes: 14 additions & 0 deletions internal/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ var (
type (
// SendChannel is a write only view of the Channel
SendChannel interface {
// Name returns the name of the Channel.
// If the Channel was retrieved from a GetSignalChannel call, Name returns the signal name.
//
// A Channel created without an explicit name will use a generated name by the SDK and
// is not deterministic.
Name() string

// Send blocks until the data is sent.
Send(ctx Context, v interface{})

Expand All @@ -80,6 +87,13 @@ type (

// ReceiveChannel is a read only view of the Channel
ReceiveChannel interface {
// Name returns the name of the Channel.
// If the Channel was retrieved from a GetSignalChannel call, Name returns the signal name.
//
// A Channel created without an explicit name will use a generated name by the SDK and
// is not deterministic.
Name() string

// Receive blocks until it receives a value, and then assigns the received value to the provided pointer.
// Returns false when Channel is closed.
// Parameter valuePtr is a pointer to the expected data structure to be received. For example:
Expand Down

0 comments on commit 25f450f

Please sign in to comment.