summaryrefslogtreecommitdiff
path: root/SmartDeviceLinkTests/SDLRPCResponseOperationSpec.m
blob: 4805eeba23c529880691525340d80528dbb32ca9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//
//  SDLRPCResponseOperationSpec.m
//  SmartDeviceLinkTests
//
//  Created by Nicole on 2/20/19.
//  Copyright © 2019 smartdevicelink. All rights reserved.
//

#import <Quick/Quick.h>
#import <Nimble/Nimble.h>

#import "SDLGetAppServiceDataResponse.h"
#import "SDLOnAppServiceData.h"
#import "SDLRPCResponseOperation.h"
#import "TestConnectionManager.h"

QuickSpecBegin(SDLRPCResponseOperationSpec)

describe(@"sending responses and notifications", ^{
    __block TestConnectionManager *testConnectionManager = nil;
    __block SDLRPCResponseOperation *testOperation = nil;
    __block NSOperationQueue *testOperationQueue = nil;

    beforeEach(^{
        testOperation = nil;
        testConnectionManager = [[TestConnectionManager alloc] init];

        testOperationQueue = [[NSOperationQueue alloc] init];
        testOperationQueue.name = @"com.sdl.RPCResponse.testqueue";
        testOperationQueue.maxConcurrentOperationCount = 3;
    });

    context(@"when a single request succeeds", ^{
        __block __kindof SDLRPCMessage *sendRPC = nil;

        beforeEach(^{
            sendRPC = [[SDLGetAppServiceDataResponse alloc] initWithAppServiceData:[[SDLAppServiceData alloc] init]];
        });

        it(@"should correctly send the rpc", ^{
            testOperation = [[SDLRPCResponseOperation alloc] initWithConnectionManager:testConnectionManager rpc:sendRPC];

            [testOperationQueue addOperation:testOperation];
            [NSThread sleepForTimeInterval:0.1];

            expect(testConnectionManager.receivedRequests).toEventually(contain(sendRPC));
        });
    });

    context(@"when multiple request succeed", ^{
        __block NSMutableArray< __kindof SDLRPCMessage *> *sendRPCs = nil;
        __block int rpcCount = (int)testOperationQueue.maxConcurrentOperationCount + 3;

        beforeEach(^{
            sendRPCs = [NSMutableArray array];
            for (int i = 0; i < rpcCount; i += 1) {
                [sendRPCs addObject:[[SDLGetAppServiceDataResponse alloc] initWithAppServiceData:[[SDLAppServiceData alloc] init]]];
            }
        });

        it(@"should correctly send all of the rpcs", ^{
            for (int i = 0; i < rpcCount; i += 1) {
                testOperation = [[SDLRPCResponseOperation alloc] initWithConnectionManager:testConnectionManager rpc:sendRPCs[i]];
                [testOperationQueue addOperation:testOperation];
            }

            [NSThread sleepForTimeInterval:0.1];

            expect(testConnectionManager.receivedRequests.count).toEventually(equal(rpcCount));
            expect(testConnectionManager.receivedRequests).toEventually(equal(sendRPCs));
        });
    });

    context(@"when a requst is cancelled", ^{
        __block __kindof SDLRPCMessage *sendRPC = nil;

        beforeEach(^{
            sendRPC = [[SDLGetAppServiceDataResponse alloc] initWithAppServiceData:[[SDLAppServiceData alloc] init]];
        });

        it(@"should not send the rpc", ^{
            testOperation = [[SDLRPCResponseOperation alloc] initWithConnectionManager:testConnectionManager rpc:sendRPC];

            [testOperationQueue addOperation:testOperation];
            [testOperationQueue cancelAllOperations];

            [NSThread sleepForTimeInterval:0.1];

            expect(testConnectionManager.receivedRequests).toEventually(beEmpty());
        });
    });
});

QuickSpecEnd