client/tests/browser_asset_cache.c (6894B)
1 #define _GNU_SOURCE 2 #include "browser_asset_cache.h" 3 #include <assert.h> 4 #include <errno.h> 5 #include <fcntl.h> 6 #include <pthread.h> 7 #include <stdio.h> 8 #include <stdlib.h> 9 #include <string.h> 10 #include <sys/stat.h> 11 #include <unistd.h> 12 13 static unsigned reads; 14 static off_t failed_offset = -1; 15 static ino_t prefetch_inode; 16 static void *publish_during_read(void *argument) 17 { 18 off_t offset = *(off_t *)argument; 19 unsigned char *bytes = calloc(1, 1048576); 20 assert(bytes); 21 bytes[0] = 'p'; 22 browser_asset_cache_store(prefetch_inode, offset, bytes, 1048576); 23 return NULL; 24 } 25 ssize_t __real_pread(int descriptor, void *buffer, size_t size, off_t offset); 26 ssize_t __wrap_pread(int descriptor, void *buffer, size_t size, off_t offset) 27 { 28 ++reads; 29 if (offset == failed_offset) { errno = EIO; return -1; } 30 if (prefetch_inode) { 31 pthread_t thread; 32 assert(!pthread_create(&thread, NULL, publish_during_read, &offset)); 33 assert(!pthread_join(thread, NULL)); 34 } 35 return __real_pread(descriptor, buffer, size, offset); 36 } 37 38 static void boundaries_and_positions(int first, int second, ino_t inode, off_t size) 39 { 40 unsigned char bytes[16] = {0}; 41 assert(lseek(first, 1048574, SEEK_SET) == 1048574); 42 unsigned before = reads; 43 44 assert(browser_asset_read_locked(first, inode, size, bytes, 6) == 6); 45 46 assert(!memcmp(bytes, "ABCDEF", 6)); 47 assert(reads == before + 2); 48 assert(lseek(first, 0, SEEK_CUR) == 1048580); 49 assert(lseek(second, 0, SEEK_CUR) == 0); 50 51 // Independent handles share bytes, while retaining their own file positions. 52 before = reads; 53 assert(browser_asset_read_locked(second, inode, size, bytes, 5) == 5); 54 assert(!memcmp(bytes, "hello", 5)); 55 assert(reads == before); 56 assert(lseek(second, 0, SEEK_CUR) == 5); 57 assert(lseek(first, 0, SEEK_CUR) == 1048580); 58 59 assert(lseek(first, size - 2, SEEK_SET) == size - 2); 60 assert(browser_asset_read_locked(first, inode, size, bytes, sizeof(bytes)) == 2); 61 assert(!memcmp(bytes, "yz", 2)); 62 before = reads; 63 assert(browser_asset_read_locked(first, inode, size, bytes, 1) == 0); 64 assert(browser_asset_read_locked(first, inode, size, NULL, 0) == 0); 65 assert(lseek(first, size + 10, SEEK_SET) == size + 10); 66 assert(browser_asset_read_locked(first, inode, size, bytes, 1) == 0); 67 assert(reads == before); 68 } 69 70 static void failed_fill_can_be_retried(int descriptor, ino_t inode, off_t size) 71 { 72 unsigned char bytes[4] = {9, 9, 9, 9}; 73 assert(lseek(descriptor, 2097150, SEEK_SET) == 2097150); 74 failed_offset = 2097152; 75 76 assert(browser_asset_read_locked(descriptor, inode, size, bytes, sizeof(bytes)) == 2); 77 78 assert(bytes[0] == 0 && bytes[1] == 0 && bytes[2] == 9); 79 assert(lseek(descriptor, 0, SEEK_CUR) == 2097152); 80 assert(browser_asset_read_locked(descriptor, inode, size, bytes, 2) == -1); 81 assert(errno == EIO && lseek(descriptor, 0, SEEK_CUR) == 2097152); 82 failed_offset = -1; 83 assert(browser_asset_read_locked(descriptor, inode, size, bytes, 2) == 2); 84 assert(bytes[0] == 0 && bytes[1] == 0); 85 } 86 87 static void different_files_do_not_share_chunks(int descriptor, ino_t inode, off_t size) 88 { 89 FILE *other = tmpfile(); 90 assert(other && fwrite("world", 1, 5, other) == 5 && !fflush(other)); 91 struct stat info; 92 assert(!fstat(fileno(other), &info) && info.st_ino != inode); 93 assert(lseek(fileno(other), 0, SEEK_SET) == 0); 94 unsigned char bytes[5]; 95 96 assert(browser_asset_read_locked(fileno(other), info.st_ino, 5, bytes, 5) == 5); 97 assert(!memcmp(bytes, "world", 5)); 98 assert(lseek(descriptor, 0, SEEK_SET) == 0); 99 assert(browser_asset_read_locked(descriptor, inode, size, bytes, 5) == 5); 100 101 assert(!memcmp(bytes, "hello", 5)); 102 assert(!fclose(other)); 103 } 104 105 static void startup_chunks_survive_until_budget_eviction(int descriptor, ino_t inode, off_t size) 106 { 107 unsigned char byte; 108 for (unsigned index = 0; index < 185; ++index) { 109 assert(lseek(descriptor, index * 1048576, SEEK_SET) == index * 1048576); 110 assert(browser_asset_read_locked(descriptor, inode, size, &byte, 1) == 1); 111 } 112 unsigned before = reads; 113 114 // The whole startup set remains resident, well beyond the old JS cache budget. 115 for (unsigned index = 0; index < 185; ++index) { 116 assert(lseek(descriptor, index * 1048576, SEEK_SET) == index * 1048576); 117 assert(browser_asset_read_locked(descriptor, inode, size, &byte, 1) == 1); 118 } 119 assert(reads == before); 120 121 // Keep chunk zero recent, then force eviction of the oldest chunks. 122 assert(lseek(descriptor, 0, SEEK_SET) == 0); 123 assert(browser_asset_read_locked(descriptor, inode, size, &byte, 1) == 1); 124 for (unsigned index = 185; index <= 256; ++index) { 125 assert(lseek(descriptor, index * 1048576, SEEK_SET) == index * 1048576); 126 assert(browser_asset_read_locked(descriptor, inode, size, &byte, 1) == 1); 127 } 128 before = reads; 129 assert(lseek(descriptor, 0, SEEK_SET) == 0); 130 assert(browser_asset_read_locked(descriptor, inode, size, &byte, 1) == 1); 131 assert(byte == 'h' && reads == before); 132 assert(lseek(descriptor, 1048576, SEEK_SET) == 1048576); 133 assert(browser_asset_read_locked(descriptor, inode, size, &byte, 1) == 1); 134 assert(byte == 'C' && reads == before + 1); 135 } 136 137 static void prefetch_can_publish_while_a_demand_read_waits(int descriptor, ino_t inode, off_t size) 138 { 139 unsigned char bytes[5]; 140 assert(lseek(descriptor, 100 * 1048576, SEEK_SET) == 100 * 1048576); 141 unsigned before = reads; 142 prefetch_inode = inode; 143 144 assert(browser_asset_read_locked(descriptor, inode, size, bytes, 5) == 5); 145 146 assert(bytes[0] == 'p' && reads == before + 1); 147 prefetch_inode = 0; 148 assert(lseek(descriptor, 100 * 1048576, SEEK_SET) == 100 * 1048576); 149 assert(browser_asset_read_locked(descriptor, inode, size, bytes, 5) == 5); 150 assert(bytes[0] == 'p' && reads == before + 1); 151 } 152 153 int main(void) 154 { 155 char path[] = "/tmp/ao-asset-cache-XXXXXX"; 156 int first = mkstemp(path), second = open(path, O_RDONLY); 157 assert(first >= 0 && second >= 0 && !unlink(path)); 158 off_t size = 257 * 1048576 + 3; 159 assert(!ftruncate(first, size)); 160 assert(pwrite(first, "hello", 5, 0) == 5); 161 assert(pwrite(first, "ABCDEF", 6, 1048574) == 6); 162 assert(pwrite(first, "xyz", 3, size - 3) == 3); 163 struct stat info; 164 assert(!fstat(first, &info)); 165 166 boundaries_and_positions(first, second, info.st_ino, size); 167 failed_fill_can_be_retried(first, info.st_ino, size); 168 different_files_do_not_share_chunks(first, info.st_ino, size); 169 prefetch_can_publish_while_a_demand_read_waits(first, info.st_ino, size); 170 startup_chunks_survive_until_budget_eviction(first, info.st_ino, size); 171 172 assert(!close(first) && !close(second)); 173 puts("asset cache: direct hits, independent handles, boundaries, EOF, retry and bounded LRU passed"); 174 }