|
| 1 | +#include "MeasurementCacheUnderTest.h" |
| 2 | +#import <Foundation/Foundation.h> |
| 3 | +#include <cstring> |
| 4 | + |
| 5 | +static NSMutableArray<dispatch_block_t> *background; |
| 6 | +static NSMutableArray<dispatch_block_t> *mainQueue; |
| 7 | + |
| 8 | +static void enqueue(dispatch_queue_t queue, dispatch_block_t block) |
| 9 | +{ |
| 10 | + [(queue == dispatch_get_main_queue() ? mainQueue : background) addObject:[block copy]]; |
| 11 | +} |
| 12 | + |
| 13 | +// Control queue progress deterministically while executing the installed coordinator itself. |
| 14 | +#define dispatch_async enqueue |
| 15 | +#include "ENRMAsyncRenderCoordinator.m" |
| 16 | +#undef dispatch_async |
| 17 | + |
| 18 | +static void check(BOOL condition, const char *message) |
| 19 | +{ |
| 20 | + if (!condition) { |
| 21 | + fprintf(stderr, "FAIL: %s\n", message); |
| 22 | + exit(1); |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +static void drain(NSMutableArray<dispatch_block_t> *queue) |
| 27 | +{ |
| 28 | + while (queue.count) { |
| 29 | + dispatch_block_t block = queue.firstObject; |
| 30 | + [queue removeObjectAtIndex:0]; |
| 31 | + block(); |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +static void testCoordinator(void) |
| 36 | +{ |
| 37 | + background = [NSMutableArray new]; |
| 38 | + mainQueue = [NSMutableArray new]; |
| 39 | + ENRMAsyncRenderCoordinator *coordinator = [[ENRMAsyncRenderCoordinator alloc] initWithQueueLabel:"test"]; |
| 40 | + __block NSUInteger rendered = 0; |
| 41 | + __block NSUInteger applied = 0; |
| 42 | + __weak NSObject *obsolete; |
| 43 | + @autoreleasepool { |
| 44 | + NSObject *document = [NSObject new]; |
| 45 | + obsolete = document; |
| 46 | + [coordinator |
| 47 | + scheduleRender:^BOOL { |
| 48 | + (void)[document description]; |
| 49 | + return YES; |
| 50 | + } |
| 51 | + apply:^{}]; |
| 52 | + } |
| 53 | + for (NSUInteger revision = 1; revision <= 3; revision++) { |
| 54 | + [coordinator |
| 55 | + scheduleRender:^BOOL { |
| 56 | + rendered++; |
| 57 | + return YES; |
| 58 | + } |
| 59 | + apply:^{ applied = revision; }]; |
| 60 | + } |
| 61 | + printf("coordinator pending workers: %lu; obsolete document retained: %s\n", (unsigned long)background.count, |
| 62 | + obsolete ? "yes" : "no"); |
| 63 | + check(obsolete == nil, "superseded pending documents must be released before the worker runs"); |
| 64 | + check(background.count == 1, "a burst must enqueue one worker, not one closure per revision"); |
| 65 | + drain(background); |
| 66 | + drain(mainQueue); |
| 67 | + check(rendered == 1 && applied == 3, "only the latest pending revision may parse and apply"); |
| 68 | + |
| 69 | + NSMutableArray<NSString *> *renders = [NSMutableArray new]; |
| 70 | + NSMutableArray<NSString *> *applies = [NSMutableArray new]; |
| 71 | + [coordinator |
| 72 | + scheduleRender:^BOOL { |
| 73 | + [renders addObject:@"in-flight"]; |
| 74 | + [coordinator |
| 75 | + scheduleRender:^BOOL { |
| 76 | + [renders addObject:@"obsolete"]; |
| 77 | + return YES; |
| 78 | + } |
| 79 | + apply:^{ [applies addObject:@"obsolete"]; }]; |
| 80 | + [coordinator |
| 81 | + scheduleRender:^BOOL { |
| 82 | + [renders addObject:@"latest"]; |
| 83 | + return YES; |
| 84 | + } |
| 85 | + apply:^{ [applies addObject:@"latest"]; }]; |
| 86 | + return YES; |
| 87 | + } |
| 88 | + apply:^{ [applies addObject:@"in-flight"]; }]; |
| 89 | + drain(background); |
| 90 | + drain(mainQueue); |
| 91 | + check([renders isEqual:@[ @"in-flight", @"latest" ]], |
| 92 | + "work arriving during a render must replace the pending revision"); |
| 93 | + check([applies isEqual:@[ @"latest" ]], "a completed superseded render must never apply"); |
| 94 | + |
| 95 | + [coordinator |
| 96 | + scheduleRender:^BOOL { |
| 97 | + rendered++; |
| 98 | + return YES; |
| 99 | + } |
| 100 | + apply:^{ applied++; }]; |
| 101 | + [coordinator invalidate]; |
| 102 | + drain(background); |
| 103 | + drain(mainQueue); |
| 104 | + check(rendered == 1 && applied == 3, "recycling must discard pending parsing and application"); |
| 105 | + |
| 106 | + printf("PASS coordinator: latest pending revision, capture release, in-flight replacement, invalidation\n"); |
| 107 | +} |
| 108 | + |
| 109 | +static void testCache(void) |
| 110 | +{ |
| 111 | + using namespace facebook::react; |
| 112 | + auto &cache = MeasurementCache::shared(); |
| 113 | + CachedSize size; |
| 114 | + // Four duplicated 1 MiB strings exceed 8 MiB once key structs are included. |
| 115 | + MeasurementCacheKey keys[4]{}; |
| 116 | + for (int index = 0; index < 4; index++) |
| 117 | + keys[index].markdown = std::string(1024 * 1024, char('a' + index)); |
| 118 | + for (int index = 0; index < 3; index++) |
| 119 | + cache.set(keys[index], {double(index), 20}); |
| 120 | + check(cache.get(keys[0], size), "a hit must remain available before budget eviction"); |
| 121 | + cache.set(keys[3], {3, 20}); |
| 122 | + check(!cache.get(keys[1], size), "byte overflow must evict the least recently used key"); |
| 123 | + for (int index : {0, 2, 3}) |
| 124 | + check(cache.get(keys[index], size) && size.width == index, "byte eviction must retain the other measurements"); |
| 125 | + |
| 126 | + // An oversized key must bypass storage without flushing useful entries. |
| 127 | + for (auto field : {&MeasurementCacheKey::markdown, &MeasurementCacheKey::lineBreakStrategyIOS, |
| 128 | + &MeasurementCacheKey::writingDirection, &MeasurementCacheKey::ellipsizeMode, |
| 129 | + &MeasurementCacheKey::linkRegexKey, &MeasurementCacheKey::inlineCodeLinkRegexKey}) { |
| 130 | + MeasurementCacheKey huge{}; |
| 131 | + huge.*field = std::string(4 * 1024 * 1024, 'z'); |
| 132 | + cache.set(huge, {99, 99}); |
| 133 | + check(!cache.get(huge, size), "8 MiB includes both copies of every string and key structs"); |
| 134 | + } |
| 135 | + for (int index : {0, 2, 3}) |
| 136 | + check(cache.get(keys[index], size), "oversized keys must preserve useful measurements"); |
| 137 | + |
| 138 | + cache.set(keys[0], {123, 456}); |
| 139 | + cache.removeMatchingMarkdown(keys[2].markdown); |
| 140 | + cache.set(keys[1], {1, 20}); |
| 141 | + check(cache.get(keys[0], size) && size.width == 123 && size.height == 456, |
| 142 | + "updates and removals must release or reuse capacity without spurious eviction"); |
| 143 | + check(cache.get(keys[3], size) && cache.get(keys[1], size), "removed capacity must be reusable"); |
| 144 | + check(!cache.get(keys[2], size), "removed measurements must stay absent"); |
| 145 | + for (const auto &key : keys) |
| 146 | + cache.removeMatchingMarkdown(key.markdown); |
| 147 | + |
| 148 | + MeasurementCacheKey boundary{}; |
| 149 | + const size_t budget = 8 * 1024 * 1024; |
| 150 | + const size_t structs = sizeof(MeasurementCache::CacheEntry) + sizeof(MeasurementCacheKey); |
| 151 | + boundary.markdown = std::string((budget - structs) / 2, 'b'); |
| 152 | + cache.set(boundary, {8, 8}); |
| 153 | + check(cache.get(boundary, size), "a key at the 8 MiB budget must fit"); |
| 154 | + auto oversized = boundary; |
| 155 | + oversized.markdown += "b"; |
| 156 | + cache.set(oversized, {9, 9}); |
| 157 | + check(!cache.get(oversized, size), "a key above the 8 MiB budget must bypass storage"); |
| 158 | + check(cache.get(boundary, size), "a rejected boundary key must not evict the cached key"); |
| 159 | + cache.removeMatchingMarkdown(boundary.markdown); |
| 160 | + |
| 161 | + MeasurementCacheKey key{}; |
| 162 | + for (int entry = 0; entry < 512; entry++) { |
| 163 | + key.markdown = std::to_string(entry); |
| 164 | + cache.set(key, {double(entry), 1}); |
| 165 | + } |
| 166 | + key.markdown = "0"; |
| 167 | + check(cache.get(key, size), "the entry limit must still allow 512 small documents"); |
| 168 | + key.markdown = "new"; |
| 169 | + cache.set(key, {1000, 1}); |
| 170 | + key.markdown = "1"; |
| 171 | + check(!cache.get(key, size), "entry overflow must evict the least recently used document"); |
| 172 | + key.markdown = "0"; |
| 173 | + check(cache.get(key, size), "reading a cache hit must refresh its LRU position"); |
| 174 | + printf("PASS cache: 8 MiB byte eviction, oversized keys, updates/removal, 512-entry LRU\n"); |
| 175 | +} |
| 176 | + |
| 177 | +int main(int argc, const char *argv[]) |
| 178 | +{ |
| 179 | + @autoreleasepool { |
| 180 | + if (argc == 1 || strcmp(argv[1], "cache") == 0) |
| 181 | + testCache(); |
| 182 | + if (argc == 1 || strcmp(argv[1], "coordinator") == 0) |
| 183 | + testCoordinator(); |
| 184 | + } |
| 185 | +} |
0 commit comments