GMPhobosUtilTest.m 2.34 KB
Newer Older
Thierry's avatar
Thierry committed
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
//
//  GMDataCompressorTest.m
//  GMPhobos
//
//  Created by Thierry on 16/2/15.
//  Copyright © 2016年 licong. All rights reserved.
//

#import <XCTest/XCTest.h>
#import "PhobosUtil.h"

@interface GMPhobosUtilTest : XCTestCase
@property (nonatomic, retain) NSMutableArray *mockArray;
@end

@implementation GMPhobosUtilTest

- (void)setUp {
    [super setUp];
    _mockArray = [[NSMutableArray alloc] init];
    for (int i=0; i<50; i++) {
        [_mockArray addObject:@{@"app":@{@"channel":@"App Store",@"name":@"gengmei_doctor",@"version":@"1.8.0"},
                                @"created_at":@"1455529180",
                                @"device":@{@"device_id":@"E8E13EB6-030A-40A0-A55A-70E7CE288740",@"device_type":@"ios"},
                                @"params":@{@"tab_id":@"diary"},
                                @"type":@"home_click_tab",
                                @"user_id": @0,
                                @"version": @110
                                }];
    }
    // Put setup code here. This method is called before the invocation of each test method in the class.
}

- (void)tearDown {
    // Put teardown code here. This method is called after the invocation of each test method in the class.
    [super tearDown];
}

/**
 *  @brief 测试50条数据压缩速度
 *
 *  @description 经过测试50条数据压缩大约1毫秒左右,几乎不耗时
 *
 *  @since 0.0.7
 */
- (void)testDataCompressPerformance {
    [self measureBlock:^{
        [self encodeAndCompressArray:_mockArray];
    }];
}


/**
 *  @brief 发送数据测试
 *
 *  @since 0.0.7
 */
- (void)testSendDataToMars{
    XCTestExpectation *expectation = [self expectationWithDescription:@"Testing Async Method Works!"];
    
    NSData *mockData = [self encodeAndCompressArray:_mockArray];
    [PhobosUtil sendData:mockData success:^(NSInteger code) {
        [expectation fulfill];
        XCTAssertEqual(code, 200);
    }];
    
    //如果超时,则认为发送失败
    [self waitForExpectationsWithTimeout:3 handler:^(NSError *error) {
        if(error)
        {
            XCTFail(@"Expectation Failed with error: %@", error);
        }
    }];
}

- (NSData *)encodeAndCompressArray:(id)obj {
    NSData *data = [NSJSONSerialization dataWithJSONObject:obj options:0 error:nil];
    NSData *compressedData = [PhobosUtil compressData:data];
    return compressedData;
}

@end