Skip to content

Commit bd96200

Browse files
authored
Remove unnecessary context parameter for non-blocking channel communication
1 parent 3eb57dd commit bd96200

File tree

7 files changed

+32
-32
lines changed

7 files changed

+32
-32
lines changed

internal/sync/channel.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@ package sync
33
type Channel[T any] interface {
44
Send(ctx Context, v T)
55

6-
SendNonblocking(ctx Context, v T) (ok bool)
6+
SendNonblocking(v T) (ok bool)
77

88
Receive(ctx Context) (v T, ok bool)
99

10-
ReceiveNonBlocking(ctx Context) (v T, ok bool)
10+
ReceiveNonBlocking() (v T, ok bool)
1111

1212
Close()
1313
}
1414

1515
type ChannelInternal[T any] interface {
1616
Closed() bool
1717

18-
ReceiveNonBlocking(ctx Context) (v T, ok bool)
18+
ReceiveNonBlocking() (v T, ok bool)
1919

2020
// AddReceiveCallback adds a callback that is called once when a value is sent to the channel. This is similar
2121
// to the blocking `Receive` method, but is not blocking a coroutine.
@@ -100,7 +100,7 @@ func (c *channel[T]) Send(ctx Context, v T) {
100100
}
101101
}
102102

103-
func (c *channel[T]) SendNonblocking(ctx Context, v T) bool {
103+
func (c *channel[T]) SendNonblocking(v T) bool {
104104
return c.trySend(v)
105105
}
106106

@@ -139,7 +139,7 @@ func (c *channel[T]) Receive(ctx Context) (v T, ok bool) {
139139
}
140140
}
141141

142-
func (c *channel[T]) ReceiveNonBlocking(ctx Context) (T, bool) {
142+
func (c *channel[T]) ReceiveNonBlocking() (T, bool) {
143143
if v, ok, rok := c.tryReceive(); rok {
144144
return v, ok
145145
}

internal/sync/channel_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func Test_Channel_Unbuffered(t *testing.T) {
5151
cr.Execute()
5252

5353
crSend := NewCoroutine(Background(), func(ctx Context) error {
54-
c.SendNonblocking(ctx, 42)
54+
c.SendNonblocking(42)
5555

5656
return nil
5757
})
@@ -79,7 +79,7 @@ func Test_Channel_Unbuffered(t *testing.T) {
7979
require.True(t, cr.Blocked(), "coroutine should be blocked")
8080

8181
crSend := NewCoroutine(Background(), func(ctx Context) error {
82-
c.SendNonblocking(ctx, 42)
82+
c.SendNonblocking(42)
8383

8484
return nil
8585
})
@@ -177,7 +177,7 @@ func Test_Channel_Unbuffered(t *testing.T) {
177177
name: "SendNonblocking_DoesNotBlock",
178178
fn: func(t *testing.T, c *channel[int]) {
179179
cr := NewCoroutine(Background(), func(ctx Context) error {
180-
r := c.SendNonblocking(ctx, 42)
180+
r := c.SendNonblocking(42)
181181

182182
require.False(t, r)
183183

@@ -194,7 +194,7 @@ func Test_Channel_Unbuffered(t *testing.T) {
194194
name: "ReceiveNonblocking_DoesNotBlock",
195195
fn: func(t *testing.T, c *channel[int]) {
196196
cr := NewCoroutine(Background(), func(ctx Context) error {
197-
r := c.SendNonblocking(ctx, 42)
197+
r := c.SendNonblocking(42)
198198

199199
require.False(t, r)
200200

@@ -225,7 +225,7 @@ func Test_Channel_Unbuffered(t *testing.T) {
225225
})
226226
}
227227

228-
s.Execute(ctx)
228+
s.Execute()
229229
require.Equal(t, 0, r)
230230

231231
for i := 0; i < 10; i++ {
@@ -237,7 +237,7 @@ func Test_Channel_Unbuffered(t *testing.T) {
237237
}
238238

239239
for s.RunningCoroutines() > 0 {
240-
s.Execute(ctx)
240+
s.Execute()
241241
}
242242

243243
require.Equal(t, 10, r)

internal/sync/go_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func Test_Go(t *testing.T) {
2020
return nil
2121
})
2222

23-
err := s.Execute(ctx)
23+
err := s.Execute()
2424
require.NoError(t, err)
2525
require.True(t, called)
2626
}
@@ -43,7 +43,7 @@ func Test_Go_MultipleGoroutines(t *testing.T) {
4343
return nil
4444
})
4545

46-
err := s.Execute(ctx)
46+
err := s.Execute()
4747
require.NoError(t, err)
4848
require.Equal(t, 2, called)
4949
}

internal/sync/scheduler.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ type Scheduler interface {
55
NewCoroutine(ctx Context, fn func(Context) error)
66

77
// Execute executes all coroutines until they are all blocked
8-
Execute(ctx Context) error
8+
Execute() error
99

1010
RunningCoroutines() int
1111

12-
Exit(ctx Context)
12+
Exit()
1313
}
1414

1515
type scheduler struct {
@@ -28,7 +28,7 @@ func (s *scheduler) NewCoroutine(ctx Context, fn func(Context) error) {
2828
c.SetScheduler(s)
2929
}
3030

31-
func (s *scheduler) Execute(ctx Context) error {
31+
func (s *scheduler) Execute() error {
3232
allBlocked := false
3333
for !allBlocked {
3434
allBlocked = true
@@ -64,7 +64,7 @@ func (s *scheduler) RunningCoroutines() int {
6464
return len(s.coroutines)
6565
}
6666

67-
func (s *scheduler) Exit(_ Context) {
67+
func (s *scheduler) Exit() {
6868
for _, c := range s.coroutines {
6969
c.Exit()
7070
}

internal/sync/scheduler_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ func Test_Scheduler(t *testing.T) {
2323

2424
require.Equal(t, 0, hit)
2525

26-
s.Execute(ctx)
26+
s.Execute()
2727
require.Equal(t, 1, hit)
2828
require.Equal(t, 1, s.RunningCoroutines())
2929

3030
// Coroutine is finished
31-
s.Execute(ctx)
31+
s.Execute()
3232
require.Equal(t, 1, hit)
3333
require.Equal(t, 0, s.RunningCoroutines())
3434
}
@@ -66,7 +66,7 @@ func Test_Scheduler_OneCoroutineAtATime(t *testing.T) {
6666
})
6767

6868
for i := 0; i < 10; i++ {
69-
s.Execute(ctx)
69+
s.Execute()
7070
}
7171

7272
require.Equal(t, 0, s.RunningCoroutines())
@@ -93,7 +93,7 @@ func Test_Scheduler_ExecuteUntilBlocked(t *testing.T) {
9393
return nil
9494
})
9595

96-
s.Execute(ctx)
96+
s.Execute()
9797

9898
require.Equal(t, 4, hits)
9999
}
@@ -129,7 +129,7 @@ func Test_Scheduler_ExecuteUntilAllBlocked(t *testing.T) {
129129
return nil
130130
})
131131

132-
s.Execute(ctx)
132+
s.Execute()
133133
require.Equal(t, 1, s.RunningCoroutines())
134134
require.Equal(t, 6, hits)
135135
}
@@ -147,12 +147,12 @@ func Test_Scheduler_Exit(t *testing.T) {
147147
}
148148
})
149149

150-
s.Execute(ctx)
150+
s.Execute()
151151

152-
s.Exit(ctx)
152+
s.Exit()
153153

154-
s.Execute(ctx)
155-
s.Execute(ctx)
154+
s.Execute()
155+
s.Execute()
156156

157157
require.Equal(t, 1, hits)
158158
}
@@ -165,7 +165,7 @@ func Test_Scheduler_Panic(t *testing.T) {
165165
panic("something went wrong")
166166
})
167167

168-
err := s.Execute(ctx)
168+
err := s.Execute()
169169

170170
require.NotNil(t, err)
171171
require.Equal(t, "panic: something went wrong", err.Error())

internal/sync/waitgroup_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func Test_WaitGroup_Blocks(t *testing.T) {
2727
return nil
2828
})
2929

30-
s.Execute(ctx)
30+
s.Execute()
3131
require.Equal(t, 1, s.RunningCoroutines())
3232

3333
s.NewCoroutine(ctx, func(ctx Context) error {
@@ -36,7 +36,7 @@ func Test_WaitGroup_Blocks(t *testing.T) {
3636
return nil
3737
})
3838

39-
s.Execute(ctx)
39+
s.Execute()
4040
require.Equal(t, 1, s.RunningCoroutines())
4141

4242
s.NewCoroutine(ctx, func(ctx Context) error {
@@ -45,6 +45,6 @@ func Test_WaitGroup_Blocks(t *testing.T) {
4545
return nil
4646
})
4747

48-
s.Execute(ctx)
48+
s.Execute()
4949
require.Equal(t, 0, s.RunningCoroutines())
5050
}

workflow/channel.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import "github.com/cschleiden/go-workflows/internal/sync"
55
type Channel[T any] interface {
66
Send(ctx Context, v T)
77

8-
SendNonblocking(ctx Context, v T) (ok bool)
8+
SendNonblocking(v T) (ok bool)
99

1010
Receive(ctx Context) (v T, ok bool)
1111

12-
ReceiveNonBlocking(ctx Context) (v T, ok bool)
12+
ReceiveNonBlocking() (v T, ok bool)
1313

1414
Close()
1515
}

0 commit comments

Comments
 (0)