SRAutobahnUtilities.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //
  2. // Copyright (c) 2016-present, Facebook, Inc.
  3. // All rights reserved.
  4. //
  5. // This source code is licensed under the BSD-style license found in the
  6. // LICENSE file in the root directory of this source tree. An additional grant
  7. // of patent rights can be found in the PATENTS file in the same directory.
  8. //
  9. #import "SRAutobahnUtilities.h"
  10. #import "SRAutobahnOperation.h"
  11. NS_ASSUME_NONNULL_BEGIN
  12. @interface SRAutobahnUtilities : NSObject @end
  13. @implementation SRAutobahnUtilities @end
  14. ///--------------------------------------
  15. #pragma mark - Test Configuration
  16. ///--------------------------------------
  17. NSString *SRAutobahnTestAgentName(void)
  18. {
  19. return [NSBundle bundleForClass:[SRAutobahnUtilities class]].bundleIdentifier;
  20. }
  21. NSURL *SRAutobahnTestServerURL(void)
  22. {
  23. return [NSURL URLWithString:@"ws://localhost:9001"];
  24. }
  25. ///--------------------------------------
  26. #pragma mark - Validation
  27. ///--------------------------------------
  28. NSDictionary<NSString *, id> *SRAutobahnTestConfiguration(void)
  29. {
  30. static NSDictionary *configuration;
  31. static dispatch_once_t onceToken;
  32. dispatch_once(&onceToken, ^{
  33. NSURL *configurationURL = [[NSBundle bundleForClass:[SRAutobahnUtilities class]] URLForResource:@"autobahn_configuration"
  34. withExtension:@"json"];
  35. NSInputStream *readStream = [NSInputStream inputStreamWithURL:configurationURL];
  36. [readStream open];
  37. configuration = [NSJSONSerialization JSONObjectWithStream:readStream options:0 error:nil];
  38. [readStream close];
  39. });
  40. return configuration;
  41. }
  42. BOOL SRAutobahnIsValidResultBehavior(NSString *caseIdentifier, NSString *behavior)
  43. {
  44. if ([behavior isEqualToString:@"OK"]) {
  45. return YES;
  46. }
  47. NSArray *cases = SRAutobahnTestConfiguration()[behavior];
  48. for (NSString *caseId in cases) {
  49. if ([caseIdentifier hasPrefix:caseId]) {
  50. return YES;
  51. }
  52. }
  53. return NO;
  54. }
  55. ///--------------------------------------
  56. #pragma mark - Utilities
  57. ///--------------------------------------
  58. BOOL SRRunLoopRunUntil(BOOL (^predicate)(), NSTimeInterval timeout)
  59. {
  60. NSDate *timeoutDate = [NSDate dateWithTimeIntervalSinceNow:timeout];
  61. NSTimeInterval timeoutTime = [timeoutDate timeIntervalSinceReferenceDate];
  62. NSTimeInterval currentTime = [NSDate timeIntervalSinceReferenceDate];
  63. while (!predicate() && currentTime < timeoutTime) {
  64. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
  65. currentTime = [NSDate timeIntervalSinceReferenceDate];
  66. }
  67. return (currentTime <= timeoutTime);
  68. }
  69. ///--------------------------------------
  70. #pragma mark - Setup
  71. ///--------------------------------------
  72. NSUInteger SRAutobahnTestCaseCount(void)
  73. {
  74. static NSUInteger count;
  75. static dispatch_once_t onceToken;
  76. dispatch_once(&onceToken, ^{
  77. SRAutobahnOperation *operation = SRAutobahnTestCaseCountOperation(SRAutobahnTestServerURL(),
  78. SRAutobahnTestAgentName(),
  79. ^(NSInteger caseCount) {
  80. count = caseCount;
  81. });
  82. [operation start];
  83. NSCAssert([operation waitUntilFinishedWithTimeout:10], @"Timed out fetching test case count.");
  84. NSCAssert(!operation.error, @"CaseGetter should have successfully returned the number of testCases. Instead got error %@", operation.error);
  85. });
  86. return count;
  87. }
  88. NSDictionary<NSString *, id> *SRAutobahnTestCaseInfo(NSInteger caseNumber)
  89. {
  90. __block NSDictionary *caseInfo = nil;
  91. SRAutobahnOperation *operation = SRAutobahnTestCaseInfoOperation(SRAutobahnTestServerURL(), caseNumber, ^(NSDictionary * _Nullable info) {
  92. caseInfo = info;
  93. });
  94. [operation start];
  95. NSCAssert([operation waitUntilFinishedWithTimeout:10], @"Timed out fetching test case info %ld.", (long)caseNumber);
  96. NSCAssert(!operation.error, @"Updating the report should not have errored");
  97. return caseInfo;
  98. }
  99. NS_ASSUME_NONNULL_END