Ejemplo n.º 1
0
        public async Task WhereClosure()
        {
            Func <int, bool> predicate = x => x % 2 == 0;

            var service = WebSocketTestService.Create(WebSocketTestService.UnrestrictedOptions, Observable.Range(0, 5));
            var results = await service.QueryAsync(xs => from x in xs
                                                   where predicate(x)
                                                   select x);

            QactiveAssert.AreEqual(results, OnNext(0), OnNext(2), OnNext(4), OnCompleted <int>());
        }
Ejemplo n.º 2
0
        public async Task DuplexMethod()
        {
            var service = WebSocketTestService.Create(WebSocketTestService.UnrestrictedOptions, Observable.Return(new TestContext()));

            var local = new Func <int>(() => 123);

            var results = await service.QueryAsync(source => from context in source
                                                   select local());

            QactiveAssert.AreEqual(results, OnNext(123), OnCompleted <int>());
        }
Ejemplo n.º 3
0
        public async Task DuplexObservableWithNonSerializablePayload()
        {
            var service = TcpTestService.Create(TcpTestService.UnrestrictedOptions, new[] { typeof(NonSerializableObject) }, Observable.Return(new TestContext()));

            var local = Observable.Return(new NonSerializableObject());

            var results = await service.QueryAsync(source => from context in source
                                                   from value in local
                                                   select value);

            QactiveAssert.AreEqual(results, OnError <NonSerializableObject>(new SerializationException(Any.Message)));
        }
Ejemplo n.º 4
0
        public async Task Join()
        {
            var service = WebSocketTestService.Create(WebSocketTestService.UnrestrictedOptions, Observable.Range(0, 6));

            var results = await service.QueryAsync(source => from x in source
                                                   join y in Observable.Range(3, 7)
                                                   on Observable.Never <Unit>() equals Observable.Never <Unit>()
                                                   where x == y
                                                   select x + y);

            QactiveAssert.AreEqual(results, OnNext(6), OnNext(8), OnNext(10), OnCompleted <int>());
        }
Ejemplo n.º 5
0
        public async Task DuplexEnumerable()
        {
            var service = TcpTestService.Create(TcpTestService.UnrestrictedOptions, Observable.Return(new TestContext()));

            var local = Enumerable.Range(1, 5);

            var results = await service.QueryAsync(source => from context in source
                                                   from clientValue in local
                                                   where clientValue % 2 == 0
                                                   select clientValue);

            QactiveAssert.AreEqual(results, OnNext(2), OnNext(4), OnCompleted <int>());
        }
Ejemplo n.º 6
0
        public async Task DuplexObservableWithNonSerializablePayload()
        {
            var service = TestService.Create(TestService.UnrestrictedOptions, new[] { typeof(NonSerializableObject) }, Observable.Return(new TestContext()));

            var obj   = new NonSerializableObject();
            var local = Observable.Return(obj);

            var results = await service.QueryAsync(source => from context in source
                                                   from value in local
                                                   select value);

            QactiveAssert.AreEqual(results, OnNext(obj), OnCompleted <NonSerializableObject>());
        }
Ejemplo n.º 7
0
        public async Task JoinWithContext()
        {
            var service = TcpTestService.Create(TcpTestService.UnrestrictedOptions, Observable.Return(new TestContext()));

            var results = await service.QueryAsync(source => from context in source
                                                   from x in context.Range0To5
                                                   join y in Observable.Range(3, 7)
                                                   on Observable.Never <Unit>() equals Observable.Never <Unit>()
                                                   where x == y
                                                   select x + y);

            QactiveAssert.AreEqual(results, OnNext(6), OnNext(8), OnNext(10), OnCompleted <int>());
        }
Ejemplo n.º 8
0
        public async Task JoinDurationClosure()
        {
            var service       = TcpTestService.Create(TcpTestService.UnrestrictedOptions, Observable.Range(0, 6));
            var range3To7     = Observable.Range(3, 5);
            var otherDuration = Observable.Never <Unit>();

            var results = await service.QueryAsync(source => from x in source
                                                   join y in range3To7
                                                   on Observable.Never <Unit>() equals otherDuration
                                                   where x == y
                                                   select x + y);

            QactiveAssert.AreEqual(results, OnNext(6), OnNext(8), OnNext(10), OnCompleted <int>());
        }
Ejemplo n.º 9
0
        public async Task GroupJoinClosure()
        {
            var service   = WebSocketTestService.Create(WebSocketTestService.UnrestrictedOptions, Observable.Range(0, 6));
            var range3To7 = Observable.Range(3, 5);

            var results = await service.QueryAsync(source => from x in source
                                                   join y in range3To7
                                                   on Observable.Never <Unit>() equals Observable.Never <Unit>()
                                                   into ys
                                                   from y in ys.Take(3)
                                                   where x == y
                                                   select x + y);

            QactiveAssert.AreEqual(results, OnNext(6), OnNext(8), OnNext(10), OnCompleted <int>());
        }
Ejemplo n.º 10
0
        public async Task GroupJoinClosureWithContext()
        {
            var service   = TestService.Create(TestService.UnrestrictedOptions, Observable.Return(new TestContext()));
            var range3To7 = Observable.Range(3, 5);

            var results = await service.QueryAsync(source => from context in source
                                                   from x in context.Range0To5
                                                   join y in range3To7
                                                   on Observable.Never <Unit>() equals Observable.Never <Unit>()
                                                   into ys
                                                   from y in ys.Take(3)
                                                   where x == y
                                                   select x + y);

            QactiveAssert.AreEqual(results, OnNext(6), OnNext(8), OnNext(10), OnCompleted <int>());
        }
Ejemplo n.º 11
0
        public async Task DuplexSubjectAsObservable()
        {
            var service = WebSocketTestService.Create(WebSocketTestService.UnrestrictedOptions, Observable.Return(new TestContext()));

            var local = new ReplaySubject <int>();

            Enumerable.Range(1, 5).ForEach(local.OnNext);
            local.OnCompleted();

            var results = await service.QueryAsync(source => from context in source
                                                   from clientValue in local
                                                   where clientValue % 2 == 0
                                                   select clientValue);

            QactiveAssert.AreEqual(results, OnNext(2), OnNext(4), OnCompleted <int>());
        }
Ejemplo n.º 12
0
        public async Task SimpleVisitor()
        {
            var visitor = new TestExpressionVisitor();
            var service = TcpTestService.Create(QbservableServiceOptions.Default.Add(visitor), Observable.Range(1, 5));

            var results = await service.QueryAsync(source => from value in source
                                                   where value % 2 == 0
                                                   select value);

            QactiveAssert.AreEqual(results, OnNext(2), OnNext(4), OnCompleted <int>());
            QactiveAssert.AreEqual(visitor.Visited,
                                   Call <int>(typeof(Qbservable), "Where", Any.ExpressionOfType <IQbservable <int> >(), Any.LambdaExpression <Func <int, bool> >(Any.ParameterExpression <int>())),
                                   Any.ExpressionOfType <IQbservable <int> >(ExpressionType.Constant),
                                   Any.QuotedLambdaExpression <Func <int, bool> >(Any.ParameterExpression <int>()),
                                   Any.LambdaExpression <Func <int, bool> >(Any.ParameterExpression <int>()),
                                   Any.ExpressionOfType(ExpressionType.Equal),
                                   Any.ExpressionOfType(ExpressionType.Modulo),
                                   Any.ParameterExpression <int>(),
                                   Expression.Constant(2),
                                   Expression.Constant(0),
                                   Any.ParameterExpression <int>());
        }
Ejemplo n.º 13
0
        public async Task ComplexTest1()
        {
            var rootIds = new[] { "C", "A" };
            var rootIdsLocal = rootIds.Do(v => Debug.WriteLine("RootObject: " + v));
            var nodeIds = new[] { new[] { "2", "4" } }.ToObservable().Do(p => p.ForEach(id => Debug.WriteLine("Node: " + id)));

            var service = TcpTestService.Create(
                TcpTestService.UnrestrictedOptions,
                new[] { typeof(RootObject), typeof(Node), typeof(Leaf), typeof(EnumerableEx) },
                Observable.Return(new ServiceContext(new[] { new RootObject("A", 1), new RootObject("B", 10), new RootObject("C", 100), new RootObject("D", 1000) })));

            var results = await service.QueryAsync(source =>
                                                   from leafPackResult in
                                                   from leaf in
                                                   (from context in source
                                                    from ids in nodeIds
                                                    select from root in context.RootObjects.ToObservable()
                                                    where rootIdsLocal.Contains(root.Id)
                                                    from node in root[ids]
                                                    from leaf in node.Leaves
                                                    select leaf)
                                                   .Switch()
                                                   group leaf by leaf.Node into leavesByNode
                                                   from leaves in leavesByNode.Buffer(leafCountPerNode *rootIds.Length)
                                                   select new
            {
                Node     = leavesByNode.Key,
                LeafPack = (from leaf in leaves
                            orderby leaf.Tick descending
                            select leaf)
                           .Distinct(leaf => leaf.Id)
                           .Memoize(memoized => new[]
                {
                    new
                    {
                        Greens = (from leaf in memoized
                                  where leaf.IsGreen
                                  orderby leaf.Size ascending
                                  select leaf)
                                 .ToList()
                                 .AsReadOnly(),
                        Browns = (from leaf in memoized
                                  where !leaf.IsGreen
                                  orderby leaf.Size descending
                                  select leaf)
                                 .ToList()
                                 .AsReadOnly()
                    }
                })
                           .First()
            }
                                                   select new
            {
                Node          = leafPackResult.Node,
                SmallestGreen = leafPackResult.LeafPack.Greens.FirstOrDefault(),
                LargestBrown  = leafPackResult.LeafPack.Browns.FirstOrDefault()
            });

            QactiveAssert.AreEqual(results,
                                   OnNext(new { Node = new Node("2"), SmallestGreen = new Leaf("2", 6), LargestBrown = new Leaf("5", 9) }),
                                   OnNext(new { Node = new Node("4"), SmallestGreen = new Leaf("2", 6), LargestBrown = new Leaf("5", 9) }),
                                   OnCompleted(new { Node = default(Node), SmallestGreen = default(Leaf), LargestBrown = default(Leaf) }));
        }