예제 #1
0
 public void HandleEvent(Any @event, IEventContext context)
 {
     Unwrap(() =>
     {
         var obj = AnySupport.Decode(@event);
         if (!CurrentBehaviors
             .Any(behavior => GetCachedBehaviorReflection(behavior)
                  .GetEventHandler(obj.GetType())
                  .Match(handler =>
         {
             var active = true;
             var ctx = new EventBehaviorContext(context, behaviors =>
             {
                 // ReSharper disable once AccessToModifiedClosure
                 if (!active)
                 {
                     throw new InvalidOperationException("Context is not active!");
                 }
                 CurrentBehaviors = ValidateBehaviors(behaviors).ToArray();
             });
             handler.Invoke(behavior, obj, ctx);
             active = false;
             return(true);
         },
                         () => false)
                  )
             )
         {
             throw new CloudStateException(
                 $"No event handler [{obj.GetType()}] found for any of the current behaviors: {BehaviorsString}");
         }
     });
 }
예제 #2
0
 public void HandleSnapshot(Any anySnapshot, ISnapshotContext context)
 {
     Unwrap(() =>
     {
         var snapshot = AnySupport.Decode(anySnapshot);
         if (!CurrentBehaviors.Any(behavior => BehaviorReflectionCache.GetOrAdd(behavior.GetType())
                                   .GetSnapshotHandler(snapshot.GetType())
                                   .Match(handler =>
         {
             var active = true;
             var ctx = new SnapshotBehaviorContext(context, behaviors =>
             {
                 // TODO: Check sequence number override on this context is set correctly.
                 // ReSharper disable once AccessToModifiedClosure
                 if (!active)
                 {
                     throw new InvalidOperationException("Context is not active!");
                 }
                 CurrentBehaviors = ValidateBehaviors(behaviors).ToArray();
             });
             handler.Invoke(behavior, snapshot, ctx);
             active = false;
             return(true);
         }, () => false))
             )
         {
             throw new CloudStateException(
                 $"No snapshot handler found for snapshot [{snapshot.GetType()}] on any of the current behaviors [{BehaviorsString}]"
                 );
         }
     });
 }
예제 #3
0
//        [Fact(Skip = "Not ready")]
        public void SupportSerializingAndDeserializingProtobufs()
        {
            var any = AnySupport.Encode(AddLineItem);

            Assert.Equal("type.googleapis.com/" + AddLineItem.Descriptor.FullName,
                         any.TypeUrl);
            Assert.Equal(AddLineItem, AnySupport.Decode(any));
        }
예제 #4
0
        public void TestPrimitive(string name, object value, object defaultValue)
        {
            var any = AnySupport.Encode(value);
            var res = AnySupport.Decode(any);

            Assert.Equal(value, res);

            // TODO: support for encoding (null as type)
            // any = AnySupport.Encode(null);
            // res = AnySupport.Decode(any);
            // Assert.Equal(defaultValue, res);
        }
예제 #5
0
        public void TestPrimitiveBytes()
        {
            var str   = "foo";
            var bytes = ByteString.CopyFromUtf8(str);
            var any   = AnySupport.Encode(bytes);
            var res   = AnySupport.Decode(any) as ByteString;

            Assert.NotNull(res);
            Assert.Equal(bytes, res);

            var val = res.ToStringUtf8();

            Assert.Equal(str, val);
        }