Esempio n. 1
0
        public async Task Put()
        {
            var putDto = await PutSimpleDataSet();

            var stateMachineIds = new List <Guid>();

            for (var x = 0; x < 30; ++x)
            {
                stateMachineIds.Add((await _service.Put(putDto)).Instance.Id);
            }
            while (true)
            {
                var stateMachines = await _service.Search(null, null, null, null, null);

                foreach (var stateMachine in stateMachines)
                {
                    if (stateMachine.Status == StateMachineStatus.Failed)
                    {
                        throw new InvalidOperationException(stateMachine.Exception);
                    }
                }
                if (stateMachines.All(x => x.Status == StateMachineStatus.Succeeded))
                {
                    stateMachines = await _service.Search(null, null, null, null, null);

                    Assert.Equal(stateMachineIds.Count, stateMachines.Count);
                    foreach (var stateMachine in stateMachines)
                    {
                        Assert.True(stateMachineIds.Contains(stateMachine.Id));
                        Assert.Equal("SimpleStateMachine", stateMachine.Name);
                        Assert.Equal(StateMachineStatus.Succeeded, stateMachine.Status);
                        Assert.Equal(StateMachineBase.FinalStage, stateMachine.Stage);
                        Assert.Equal(2, stateMachine.StartedActors.Count);

                        Assert.Equal("CompileUserCodeActor", stateMachine.StartedActors[0].Name);
                        Assert.True(stateMachine.StartedActors[0].StartTime != DateTime.MinValue);
                        Assert.True(stateMachine.StartedActors[0].EndTime != null);
                        Assert.Equal(1, stateMachine.StartedActors[0].Inputs.Count());
                        Assert.Equal(putDto.InitialBlobs[0].Id, stateMachine.StartedActors[0].Inputs.First().Id);
                        Assert.Equal(putDto.InitialBlobs[0].Name, stateMachine.StartedActors[0].Inputs.First().Name);
                        Assert.Equal(putDto.InitialBlobs[0].Tag, stateMachine.StartedActors[0].Inputs.First().Tag);
                        Assert.Equal(4, stateMachine.StartedActors[0].Outputs.Count());
                        Assert.True(stateMachine.StartedActors[0].Outputs.Any(x => x.Name == "runner.json"));
                        Assert.True(stateMachine.StartedActors[0].Outputs.Any(x => x.Name == "Main.out"));
                        Assert.True(stateMachine.StartedActors[0].Outputs.Any(x => x.Name == "stdout.txt"));
                        Assert.True(stateMachine.StartedActors[0].Outputs.Any(x => x.Name == "stderr.txt"));
                        Assert.Equal(0, stateMachine.StartedActors[0].Exceptions.Length);
                        Assert.Equal(ActorStatus.Succeeded, stateMachine.StartedActors[0].Status);
                        Assert.Equal("CompileUserCode", stateMachine.StartedActors[0].Stage);
                        Assert.True(string.IsNullOrEmpty(stateMachine.StartedActors[0].Tag));
                        Assert.True(!string.IsNullOrEmpty(stateMachine.StartedActors[0].UsedNode));
                        Assert.True(!string.IsNullOrEmpty(stateMachine.StartedActors[0].UsedContainer));

                        Assert.Equal("RunUserCodeActor", stateMachine.StartedActors[1].Name);
                        Assert.True(stateMachine.StartedActors[1].StartTime != DateTime.MinValue);
                        Assert.True(stateMachine.StartedActors[1].EndTime != null);
                        Assert.Equal(
                            JsonConvert.SerializeObject(stateMachine.StartedActors[0].Outputs),
                            JsonConvert.SerializeObject(stateMachine.StartedActors[1].Inputs));
                        Assert.Equal(3, stateMachine.StartedActors[1].Outputs.Count());
                        Assert.True(stateMachine.StartedActors[1].Outputs.Any(x => x.Name == "runner.json"));
                        Assert.True(stateMachine.StartedActors[1].Outputs.Any(x => x.Name == "stdout.txt"));
                        Assert.True(stateMachine.StartedActors[1].Outputs.Any(x => x.Name == "stderr.txt"));
                        var blob   = stateMachine.StartedActors[1].Outputs.FindBlob("stdout.txt");
                        var stdout = (await _store.ReadBlobs(new[] { blob })).Last().Item2;
                        Assert.Equal("simple state machine is ok\r\n", Encoding.UTF8.GetString(stdout));
                        Assert.Equal(0, stateMachine.StartedActors[1].Exceptions.Length);
                        Assert.Equal(ActorStatus.Succeeded, stateMachine.StartedActors[1].Status);
                        Assert.Equal("RunUserCode", stateMachine.StartedActors[1].Stage);
                        Assert.True(string.IsNullOrEmpty(stateMachine.StartedActors[1].Tag));
                        Assert.True(!string.IsNullOrEmpty(stateMachine.StartedActors[1].UsedNode));
                        Assert.True(!string.IsNullOrEmpty(stateMachine.StartedActors[1].UsedContainer));

                        Assert.Equal(
                            JsonConvert.SerializeObject(putDto.InitialBlobs),
                            JsonConvert.SerializeObject(stateMachine.InitialBlobs));
                        Assert.Equal(
                            JsonConvert.SerializeObject(_configuration.Limitation),
                            JsonConvert.SerializeObject(stateMachine.Limitation));
                        Assert.Equal(_configuration.Name, stateMachine.FromManagementService);
                        Assert.Equal(0, stateMachine.ReRunTimes);
                        Assert.True(string.IsNullOrEmpty(stateMachine.Exception));
                        Assert.True(!string.IsNullOrEmpty(stateMachine.ExecutionKey));
                        Assert.True(stateMachine.StartTime != DateTime.MinValue);
                        Assert.True(stateMachine.EndTime != null);
                    }
                    break;
                }
                await Task.Delay(1);
            }
        }
        public async Task RunInstance()
        {
            var(stateMachineEntity, stateMachineInstanceEntity) = await PutTestInstance();

            var instance = await _store.CreateInstance(stateMachineEntity, stateMachineInstanceEntity);

            await _store.RunInstance(instance);

            var repository = new DummyRepository <StateMachineInstanceEntity, Guid>(_storage);

            while (true)
            {
                var stateMachine = await repository.QueryNoTrackingAsync(q =>
                                                                         q.FirstOrDefaultAsyncTestable(x => x.Id == stateMachineInstanceEntity.Id));

                if (stateMachine.Status == StateMachineStatus.Failed)
                {
                    throw new InvalidOperationException(stateMachine.Exception);
                }
                if (stateMachine.Status == StateMachineStatus.Succeeded)
                {
                    Assert.Equal("SimpleStateMachine", stateMachine.Name);
                    Assert.Equal(StateMachineStatus.Succeeded, stateMachine.Status);
                    Assert.Equal(StateMachineBase.FinalStage, stateMachine.Stage);
                    Assert.Equal(2, stateMachine.StartedActors.Count);

                    Assert.Equal("CompileUserCodeActor", stateMachine.StartedActors[0].Name);
                    Assert.True(stateMachine.StartedActors[0].StartTime != DateTime.MinValue);
                    Assert.True(stateMachine.StartedActors[0].EndTime != null);
                    Assert.Equal(1, stateMachine.StartedActors[0].Inputs.Count());
                    Assert.Equal(stateMachine.InitialBlobs[0].Id, stateMachine.StartedActors[0].Inputs.First().Id);
                    Assert.Equal(stateMachine.InitialBlobs[0].Name, stateMachine.StartedActors[0].Inputs.First().Name);
                    Assert.Equal(stateMachine.InitialBlobs[0].Tag, stateMachine.StartedActors[0].Inputs.First().Tag);
                    Assert.Equal(4, stateMachine.StartedActors[0].Outputs.Count());
                    Assert.True(stateMachine.StartedActors[0].Outputs.Any(x => x.Name == "runner.json"));
                    Assert.True(stateMachine.StartedActors[0].Outputs.Any(x => x.Name == "Main.out"));
                    Assert.True(stateMachine.StartedActors[0].Outputs.Any(x => x.Name == "stdout.txt"));
                    Assert.True(stateMachine.StartedActors[0].Outputs.Any(x => x.Name == "stderr.txt"));
                    Assert.Equal(0, stateMachine.StartedActors[0].Exceptions.Length);
                    Assert.Equal(ActorStatus.Succeeded, stateMachine.StartedActors[0].Status);
                    Assert.Equal("CompileUserCode", stateMachine.StartedActors[0].Stage);
                    Assert.True(string.IsNullOrEmpty(stateMachine.StartedActors[0].Tag));
                    Assert.True(!string.IsNullOrEmpty(stateMachine.StartedActors[0].UsedNode));
                    Assert.True(!string.IsNullOrEmpty(stateMachine.StartedActors[0].UsedContainer));

                    Assert.Equal("RunUserCodeActor", stateMachine.StartedActors[1].Name);
                    Assert.True(stateMachine.StartedActors[1].StartTime != DateTime.MinValue);
                    Assert.True(stateMachine.StartedActors[1].EndTime != null);
                    Assert.Equal(
                        JsonConvert.SerializeObject(stateMachine.StartedActors[0].Outputs),
                        JsonConvert.SerializeObject(stateMachine.StartedActors[1].Inputs));
                    Assert.Equal(3, stateMachine.StartedActors[1].Outputs.Count());
                    Assert.True(stateMachine.StartedActors[1].Outputs.Any(x => x.Name == "runner.json"));
                    Assert.True(stateMachine.StartedActors[1].Outputs.Any(x => x.Name == "stdout.txt"));
                    Assert.True(stateMachine.StartedActors[1].Outputs.Any(x => x.Name == "stderr.txt"));
                    var blob   = stateMachine.StartedActors[1].Outputs.FindBlob("stdout.txt");
                    var stdout = (await _store.ReadBlobs(new[] { blob })).Last().Item2;
                    Assert.Equal("simple state machine is ok\r\n", Encoding.UTF8.GetString(stdout));
                    Assert.Equal(0, stateMachine.StartedActors[1].Exceptions.Length);
                    Assert.Equal(ActorStatus.Succeeded, stateMachine.StartedActors[1].Status);
                    Assert.Equal("RunUserCode", stateMachine.StartedActors[1].Stage);
                    Assert.True(string.IsNullOrEmpty(stateMachine.StartedActors[1].Tag));
                    Assert.True(!string.IsNullOrEmpty(stateMachine.StartedActors[1].UsedNode));
                    Assert.True(!string.IsNullOrEmpty(stateMachine.StartedActors[1].UsedContainer));

                    Assert.Equal(
                        JsonConvert.SerializeObject(_configuration.Limitation),
                        JsonConvert.SerializeObject(stateMachine.Limitation));
                    Assert.Equal(_configuration.Name, stateMachine.FromManagementService);
                    Assert.Equal(0, stateMachine.ReRunTimes);
                    Assert.True(string.IsNullOrEmpty(stateMachine.Exception));
                    Assert.True(!string.IsNullOrEmpty(stateMachine.ExecutionKey));
                    Assert.True(stateMachine.StartTime != DateTime.MinValue);
                    Assert.True(stateMachine.EndTime != null);
                    break;
                }
                await Task.Delay(1);
            }
        }