예제 #1
0
        /// <summary>
        /// Creates a node in the specific state
        /// </summary>
        /// <param name="state">expected state of the returning node</param>
        /// <returns>new node instance</returns>
        private async Task <NodeMock> CreateNode(NodeState state)
        {
            var node = new NodeMock();

            switch (state)
            {
            case NodeState.Uninitialized: break;

            case NodeState.Stopped:
                await node.Initialize("{}", CancellationToken.None);

                break;

            case NodeState.Running:
                await node.Initialize("{}", CancellationToken.None);

                await node.Start(CancellationToken.None);

                break;

            case NodeState.InitializationFailed:
                node.InitializeBehavior = MockBehavior.ThrowException;
                try
                {
                    await node.Initialize("{}", CancellationToken.None);
                }
                catch (Exception)
                {
                    // This is expected to go into target state
                }

                break;

            case NodeState.Failed:
                await node.Initialize("{}", CancellationToken.None);

                await node.LetFail(CancellationToken.None);

                break;

            case NodeState.Resetting:
            case NodeState.Starting:
            case NodeState.Stopping:
            case NodeState.Initializing:
            case NodeState.Deinitializing:
                throw new NotSupportedException("transition states are not supported");

            default:
                throw new ArgumentOutOfRangeException(nameof(state), state, null);
            }

            // Cleanup node
            node.Reset();

            return(node);
        }
예제 #2
0
        /// <summary>
        /// Executes and cancels the given action
        /// </summary>
        /// <param name="node">reference to the node</param>
        /// <param name="resetEvent">fitting reset event for cancellation wait</param>
        /// <param name="action">delegate</param>
        private async Task ExecuteWithToken(NodeMock node, ManualResetEvent resetEvent, Func <INode, CancellationToken, Task> action)
        {
            using var tokenSource = new CancellationTokenSource();

            // Start method
            var executionTask = action(node, tokenSource.Token);

            // Wait until the call enters the inner handler
            resetEvent.WaitOne();

            // cancel process
            tokenSource.Cancel();

            // Wait for end of execution
            await Assert.ThrowsAsync <TaskCanceledException>(() => executionTask);
        }
        void given_a_generated_node()
        {
            before = () => _node = _kaleidoscope.Generate(10, null, 100, 100);

            it["should not be null"]     = () => _node.should_not_be_null();
            it["should have 4 children"] = () => _node.Children.Count.should_be(4);

            context["given its geometry"] = () =>
            {
                before = () => _geometry = _node.Geometry;
                it["should not be null"]      = () => _geometry.should_not_be_null();
                it["should contain 3 points"] = () => _geometry.Points.Count.should_be(3);
            };

            context["given its transformation"] = () =>
            {
                before = () => _transformation = _node.Transformation;
                it["should not be null"]      = () => _transformation.should_not_be_null();
                it["should be a translation"] = () =>
                                                _transformation.Type.should_be(TransformationMock.TransformationType.Translation);
            };

            context["given its first child"] = () =>
            {
                before = () => _node = _node.Children[0];
                it["should not be null"]     = () => _node.should_not_be_null();
                it["should have a geometry"] = () => _node.Geometry.should_not_be_null();
                it["should have a geometry with 3 points"] = () => _node.Geometry.Points.Count.should_be(3);
                it["should have a translation and flip transformation"] = () =>
                {
                    _node.Transformation.should_not_be_null();
                    _node.Transformation.Type.should_be(TransformationMock.TransformationType.Group);
                    _node.Transformation.Parameters.should_not_be_null();
                    _node.Transformation.Parameters.Length.should_be(2);
                    ((TransformationMock)_node.Transformation.Parameters[0]).Type.should_be(TransformationMock.TransformationType.Flip);
                    ((TransformationMock)_node.Transformation.Parameters[1]).Type.should_be(TransformationMock.TransformationType.Translation);
                };
            };

            // TODO: roughly check positioning
        }
        void given_a_generated_node()
        {
            before = () => _node = _kaleidoscope.Generate(10, null, 100, 100);

            it["should not be null"] = () => _node.should_not_be_null();
            it["should have 4 children"] = () => _node.Children.Count.should_be(4);

            context["given its geometry"] = () =>
            {
                before = () => _geometry = _node.Geometry;
                it["should not be null"] = () => _geometry.should_not_be_null();
                it["should contain 3 points"] = () => _geometry.Points.Count.should_be(3);
            };

            context["given its transformation"] = () =>
            {
                before = () => _transformation = _node.Transformation;
                it["should not be null"] = () => _transformation.should_not_be_null();
                it["should be a translation"] = () =>
                    _transformation.Type.should_be(TransformationMock.TransformationType.Translation);
            };

            context["given its first child"] = () =>
            {
                before = () => _node = _node.Children[0];
                it["should not be null"] = () => _node.should_not_be_null();
                it["should have a geometry"] = () => _node.Geometry.should_not_be_null();
                it["should have a geometry with 3 points"] = () => _node.Geometry.Points.Count.should_be(3);
                it["should have a translation and flip transformation"] = () =>
                {
                    _node.Transformation.should_not_be_null();
                    _node.Transformation.Type.should_be(TransformationMock.TransformationType.Group);
                    _node.Transformation.Parameters.should_not_be_null();
                    _node.Transformation.Parameters.Length.should_be(2);
                    ((TransformationMock)_node.Transformation.Parameters[0]).Type.should_be(TransformationMock.TransformationType.Flip);
                    ((TransformationMock)_node.Transformation.Parameters[1]).Type.should_be(TransformationMock.TransformationType.Translation);
                };
            };

            // TODO: roughly check positioning
        }