|
5 | 5 | "errors"
|
6 | 6 | "fmt"
|
7 | 7 | "math/rand"
|
8 |
| - "reflect" |
9 | 8 | "sync"
|
10 | 9 | "testing"
|
11 | 10 | "time"
|
@@ -211,9 +210,125 @@ func Test_Futures(t *testing.T) {
|
211 | 210 | env.ExecuteWorkflow(futureTest)
|
212 | 211 | }
|
213 | 212 |
|
214 |
| -func Test_valuePtr(t *testing.T) { |
215 |
| - slices := make([]int, 10) |
216 |
| - slicePtr := &slices |
| 213 | +func batchWorkflowAssignWithSlice(ctx internal.Context) ([]int, error) { |
| 214 | + totalSize := 5 |
| 215 | + concurrency := 2 |
| 216 | + factories := make([]func(ctx internal.Context) internal.Future, totalSize) |
| 217 | + for i := 0; i < totalSize; i++ { |
| 218 | + i := i |
| 219 | + factories[i] = func(ctx internal.Context) internal.Future { |
| 220 | + aCtx := internal.WithActivityOptions(ctx, internal.ActivityOptions{ |
| 221 | + ScheduleToStartTimeout: time.Second * 10, |
| 222 | + StartToCloseTimeout: time.Second * 10, |
| 223 | + }) |
| 224 | + return internal.ExecuteActivity(aCtx, batchActivity, i) |
| 225 | + } |
| 226 | + } |
| 227 | + |
| 228 | + batchFuture, err := NewBatchFuture(ctx, concurrency, factories) |
| 229 | + if err != nil { |
| 230 | + return nil, err |
| 231 | + } |
217 | 232 |
|
218 |
| - fmt.Println(reflect.ValueOf(slicePtr).Elem().Len()) |
| 233 | + var valuePtr []int |
| 234 | + if err := batchFuture.Get(ctx, &valuePtr); err != nil { |
| 235 | + return nil, err |
| 236 | + } |
| 237 | + return valuePtr, nil |
| 238 | +} |
| 239 | + |
| 240 | +func batchWorkflowAssignWithSliceOfPointers(ctx internal.Context) ([]int, error) { |
| 241 | + totalSize := 5 |
| 242 | + concurrency := 2 |
| 243 | + factories := make([]func(ctx internal.Context) internal.Future, totalSize) |
| 244 | + for i := 0; i < totalSize; i++ { |
| 245 | + i := i |
| 246 | + factories[i] = func(ctx internal.Context) internal.Future { |
| 247 | + aCtx := internal.WithActivityOptions(ctx, internal.ActivityOptions{ |
| 248 | + ScheduleToStartTimeout: time.Second * 10, |
| 249 | + StartToCloseTimeout: time.Second * 10, |
| 250 | + }) |
| 251 | + return internal.ExecuteActivity(aCtx, batchActivity, i) |
| 252 | + } |
| 253 | + } |
| 254 | + batchFuture, err := NewBatchFuture(ctx, concurrency, factories) |
| 255 | + if err != nil { |
| 256 | + return nil, err |
| 257 | + } |
| 258 | + var valuePtr []*int |
| 259 | + if err := batchFuture.Get(ctx, &valuePtr); err != nil { |
| 260 | + return nil, err |
| 261 | + } |
| 262 | + |
| 263 | + var result []int |
| 264 | + for _, v := range valuePtr { |
| 265 | + result = append(result, *v) |
| 266 | + } |
| 267 | + return result, nil |
| 268 | +} |
| 269 | + |
| 270 | +func batchWorkflowAssignWithNil(ctx internal.Context) ([]int, error) { |
| 271 | + totalSize := 5 |
| 272 | + concurrency := 2 |
| 273 | + factories := make([]func(ctx internal.Context) internal.Future, totalSize) |
| 274 | + for i := 0; i < totalSize; i++ { |
| 275 | + i := i |
| 276 | + factories[i] = func(ctx internal.Context) internal.Future { |
| 277 | + aCtx := internal.WithActivityOptions(ctx, internal.ActivityOptions{ |
| 278 | + ScheduleToStartTimeout: time.Second * 10, |
| 279 | + StartToCloseTimeout: time.Second * 10, |
| 280 | + }) |
| 281 | + return internal.ExecuteActivity(aCtx, batchActivity, i) |
| 282 | + } |
| 283 | + } |
| 284 | + |
| 285 | + batchFuture, err := NewBatchFuture(ctx, concurrency, factories) |
| 286 | + if err != nil { |
| 287 | + return nil, err |
| 288 | + } |
| 289 | + |
| 290 | + var valuePtr []int |
| 291 | + if err := batchFuture.Get(ctx, nil); err != nil { |
| 292 | + return nil, err |
| 293 | + } |
| 294 | + return valuePtr, nil |
| 295 | +} |
| 296 | + |
| 297 | +func Test_BatchFuture_Get(t *testing.T) { |
| 298 | + tests := []struct { |
| 299 | + name string |
| 300 | + workflow func(ctx internal.Context) ([]int, error) |
| 301 | + want interface{} |
| 302 | + }{ |
| 303 | + { |
| 304 | + name: "success with nil slice", |
| 305 | + workflow: batchWorkflowAssignWithSlice, |
| 306 | + want: []int{0, 1, 2, 3, 4}, |
| 307 | + }, |
| 308 | + { |
| 309 | + name: "success with non-nil slice", |
| 310 | + workflow: batchWorkflowAssignWithSliceOfPointers, |
| 311 | + want: []int{0, 1, 2, 3, 4}, |
| 312 | + }, |
| 313 | + { |
| 314 | + name: "success with nil", |
| 315 | + workflow: batchWorkflowAssignWithNil, |
| 316 | + want: []int(nil), |
| 317 | + }, |
| 318 | + } |
| 319 | + |
| 320 | + for _, tt := range tests { |
| 321 | + t.Run(tt.name, func(t *testing.T) { |
| 322 | + testSuite := &testsuite.WorkflowTestSuite{} |
| 323 | + env := testSuite.NewTestWorkflowEnvironment() |
| 324 | + env.RegisterWorkflow(tt.workflow) |
| 325 | + env.RegisterActivity(batchActivity) |
| 326 | + env.ExecuteWorkflow(tt.workflow) |
| 327 | + assert.True(t, env.IsWorkflowCompleted()) |
| 328 | + assert.Nil(t, env.GetWorkflowError()) |
| 329 | + var result []int |
| 330 | + assert.Nil(t, env.GetWorkflowResult(&result)) |
| 331 | + assert.Equal(t, tt.want, result) |
| 332 | + }) |
| 333 | + } |
219 | 334 | }
|
0 commit comments