vlingo-net / xoom-net-common

These are just a few common tools shared across various vlingo .NET projects.
Mozilla Public License 2.0
7 stars 9 forks source link

Cast exception in AndThenToInternal #42

Closed tjaskula closed 4 years ago

tjaskula commented 5 years ago

While implementing Vlingo.Http this piece of code below has produced cast exception error:

_stage.ActorOf<IUser>(_stage.World.AddressFactory.From(userId))
                .AndThenTo(user => user.WithName(new Name(nameData.Given, nameData.Family)))
                .OtherwiseConsume(noUser => Completes.With(Response.Of(Response.ResponseStatus.NotFound, UserLocation(userId))))
                .AndThenConsume(userState => {
                _repository.Save(userState);
                Completes.With(Response.Of(Response.ResponseStatus.Ok, JsonSerialization.Serialized(UserData.From(userState))));
            });

And the error produced is as follows:

consoleLogger[Error] [Exception]: Unable to cast object of type 'Action`1[Vlingo.Common.ICompletes`1[Vlingo.Http.Tests.Sample.User.Model.UserState],Vlingo.Common.ICompletes`1[Vlingo.Http.Tests.Sample.User.Model.UserState]]' to type 'Action`1[Vlingo.Http.Tests.Sample.User.Model.IUser,Vlingo.Http.Tests.Sample.User.Model.IUser]'.
consoleLogger[Error] [StackTrace]:    at Vlingo.Common.BasicCompletes`1.AndThenToInternal[TF,TO](TimeSpan timeout, Optional`1 failedOutcomeValue, Func`2 function)
   at Vlingo.Common.BasicCompletes`1.AndThenTo[TO](Func`2 function)
   at Vlingo.Http.Tests.Sample.User.UserResource.ChangeName(String userId, NameData nameData) in C:\Sources\GitHub\vlingo-net-http\src\Vlingo.Http.Tests\Sample\User\UserResource.cs:line 52
tjaskula commented 5 years ago

I'm able to reproduce the bug with this simple test:

[Fact]
        public void TestAndThenToWithComplexTypes()
        {
            var completes = new BasicCompletes<IUser>(new Scheduler());

            completes
                .AndThenTo(user => user.WithName("Tomasz"))
                .OtherwiseConsume(noUser => completes.With(new UserState(string.Empty, string.Empty, string.Empty)))
                .AndThenConsume(userState => {
                    completes.With(userState);
                });

            var completed = completes.Await<UserState>();

            Assert.Equal("Tomasz", completed.Name);
        }
tjaskula commented 4 years ago

Resolved by #51