Objective-J Test API 0.1.0
|
00001 @import "OJTestRunnerText.j" 00002 00003 @implementation OJTestRunnerTextParallel : OJTestRunnerText 00004 { 00005 CPArray threadPool; 00006 } 00007 00008 - (id)init 00009 { 00010 self = [super init]; 00011 if(self) 00012 { 00013 threadPool = [ 00014 [[OJThread alloc] initWithDelegate:self], 00015 [[OJThread alloc] initWithDelegate:self], 00016 [[OJThread alloc] initWithDelegate:self], 00017 [[OJThread alloc] initWithDelegate:self], 00018 [[OJThread alloc] initWithDelegate:self], 00019 [[OJThread alloc] initWithDelegate:self], 00020 [[OJThread alloc] initWithDelegate:self], 00021 [[OJThread alloc] initWithDelegate:self], 00022 [[OJThread alloc] initWithDelegate:self], 00023 [[OJThread alloc] initWithDelegate:self] 00024 ]; 00025 } 00026 return self; 00027 } 00028 00029 - (void)startWithArguments:(CPArray)args 00030 { 00031 while(args.length !== 0) 00032 { 00033 while([self threadsAvailable]) 00034 { 00035 if (args.length === 0) break; 00036 00037 var testCaseFile = [self nextTest:args]; 00038 00039 if(!testCaseFile || testCaseFile == "") break; 00040 00041 var matches = testCaseFile.match(/([^\/]+)\.j$/); 00042 00043 if (matches) 00044 { 00045 system.stderr.write(matches[1]).flush(); 00046 var testCaseClass = matches[1]; 00047 00048 [self beforeRequire]; 00049 require(testCaseFile); 00050 var suite = [self getTest:testCaseClass]; 00051 00052 var runTestFunction = function() { 00053 [self run:suite]; 00054 }; 00055 00056 var thread = [self firstAvailableThread]; 00057 [thread setStartFunction:runTestFunction]; 00058 [thread start]; 00059 } 00060 else 00061 system.stderr.write("Skipping " + testCaseFile + ": not an Objective-J source file.\n").flush(); 00062 } 00063 } 00064 00065 [self report]; 00066 } 00067 00068 - (BOOL)threadsAvailable 00069 { 00070 for(var i = 0, n = threadPool.length; i < n; i++) 00071 if (![threadPool[i] isRunning]) 00072 return YES; 00073 00074 return NO; 00075 } 00076 00077 - (OJThread)firstAvailableThread 00078 { 00079 for(var i = 0, n = threadPool.length; i < n; i++) 00080 if (![threadPool[i] isRunning]) 00081 return threadPool[i]; 00082 00083 return nil; 00084 } 00085 00086 - (void)threadFinished:(OJThread)aThread 00087 { 00088 } 00089 00090 - (BOOL)threadsDone 00091 { 00092 for(var i = 0, n = threadPool.length; i < n; i++) 00093 if ([threadPool[i] isRunning]) 00094 return NO; 00095 00096 return YES; 00097 } 00098 00099 - (void)report 00100 { 00101 var startTime = new Date(); 00102 while(![self threadsDone]) 00103 { 00104 if ([self hasWaited:60 since:startTime]) { 00105 system.print("Test timed out!"); 00106 break; 00107 } 00108 } 00109 00110 [super report]; 00111 } 00112 00113 - (BOOL)hasWaited:(CPNumber)seconds since:(CPDate)startTime 00114 { 00115 return seconds < [[CPDate date] timeIntervalSinceDate:startTime]; 00116 } 00117 00118 @end 00119 00120 @implementation OJThread : CPObject 00121 { 00122 BOOL isRunning @accessors(readonly); 00123 Function startFunction @accessors; 00124 id delegate; 00125 } 00126 00127 - (id)initWithDelegate:(id)aDelegate 00128 { 00129 self = [super init]; 00130 if(self) 00131 { 00132 isRunning = NO; 00133 startFunction = function(){}; 00134 delegate = aDelegate; 00135 } 00136 return self; 00137 } 00138 00139 - (void)start 00140 { 00141 isRunning = true; 00142 var ojThread = self; 00143 var thread = new java.lang.Thread(function() 00144 { 00145 startFunction(); 00146 [delegate threadFinished:ojThread]; 00147 isRunning = false; 00148 }); 00149 thread.start(); 00150 } 00151 00152 @end