1module remote23import os4import time56fn scratch(name string) !string {7 dir := os.join_path(os.temp_dir(), 'koh3_remote_${os.getpid()}_${name}')8 os.rmdir_all(dir) or {}9 os.mkdir_all(dir, os.MkdirParams{})!10 return dir11}1213fn test_a_missing_key_is_not_an_error() {14 base := scratch('missing')!15 defer {16 os.rmdir_all(base) or {}17 }18 mut backend := new_folder(base)!19 object := backend.get('repo/nothing')!20 assert !object.found21 assert object.body.len == 022}2324fn test_put_if_absent_refuses_a_second_write() {25 base := scratch('absent')!26 defer {27 os.rmdir_all(base) or {}28 }29 mut backend := new_folder(base)!30 assert backend.put_if_absent('repo/objects/abc', 'first'.bytes())!31 assert !backend.put_if_absent('repo/objects/abc', 'second'.bytes())!32 object := backend.get('repo/objects/abc')!33 assert object.body == 'first'.bytes()34 assert object.tag == tag_of('first'.bytes())35}3637fn test_put_if_match_only_swaps_the_version_that_was_read() {38 base := scratch('match')!39 defer {40 os.rmdir_all(base) or {}41 }42 mut backend := new_folder(base)!43 backend.put_if_absent('repo/head', 'one'.bytes())!44 tag := backend.get('repo/head')!.tag4546 47 assert !backend.put_if_match('repo/head', 'two'.bytes(), 'a-stale-tag')!48 assert backend.get('repo/head')!.body == 'one'.bytes()4950 51 assert backend.put_if_match('repo/head', 'two'.bytes(), tag)!52 assert backend.get('repo/head')!.body == 'two'.bytes()5354 55 assert !backend.put_if_match('repo/fresh', 'x'.bytes(), tag)!56 assert backend.put_if_match('repo/created', 'x'.bytes(), '')!57 assert backend.get('repo/created')!.found58}5960fn test_list_walks_the_prefix() {61 base := scratch('list')!62 defer {63 os.rmdir_all(base) or {}64 }65 mut backend := new_folder(base)!66 backend.put_if_absent('repo/saves/aaa', 'a'.bytes())!67 backend.put_if_absent('repo/saves/bbb', 'b'.bytes())!68 backend.put_if_absent('repo/objects/ccc', 'c'.bytes())!69 assert backend.list('repo/saves')! == ['repo/saves/aaa', 'repo/saves/bbb']70 assert backend.list('repo/nothing')!.len == 07172 73 74 75 os.write_file(os.join_path(base, 'repo/saves/ddd.tmp-4242'), 'half')!76 os.write_file(os.join_path(base, 'repo/saves/aaa.lock'), '4242')!77 os.write_file(os.join_path(base, 'repo/saves/aaa.lock.4242.1790000000'), '4242')!78 assert backend.list('repo/saves')! == ['repo/saves/aaa', 'repo/saves/bbb']79}8081fn test_delete_removes_the_key() {82 base := scratch('delete')!83 defer {84 os.rmdir_all(base) or {}85 }86 mut backend := new_folder(base)!87 backend.put_if_absent('repo/objects/abc', 'gone'.bytes())!88 backend.delete('repo/objects/abc')!89 assert !backend.get('repo/objects/abc')!.found90}9192fn test_check_passes_on_a_working_remote_and_leaves_nothing_behind() {93 base := scratch('check')!94 defer {95 os.rmdir_all(base) or {}96 }97 mut backend := new_folder(base)!98 check(mut backend)!99 assert backend.list('')!.len == 0100101 102 103 backend.put_if_absent(probe_key, 'in flight'.bytes())!104 check(mut backend)!105 assert backend.get(probe_key)!.found106 assert backend.list('')! == [probe_key]107}108109fn test_exclusive_create_covers_filesystems_without_hard_links() {110 dir := scratch('exclusive')!111 defer {112 os.rmdir_all(dir) or {}113 }114 path := os.join_path(dir, 'key')115 assert create_exclusive(path, 'first'.bytes())!116 assert os.read_file(path)! == 'first'117 assert !create_exclusive(path, 'second'.bytes())!118 assert os.read_file(path)! == 'first'119120 121 guard := os.join_path(dir, 'key.lock')122 assert take_lock(guard)!123 assert !take_lock(guard)!124 assert os.read_file(guard)! == '${os.getpid()}'125 release_lock(guard)126 assert take_lock(guard)!127 release_lock(guard)128}129130fn test_a_stale_lock_is_cleared_only_when_its_writer_is_gone() {131 base := scratch('stale_lock')!132 defer {133 os.rmdir_all(base) or {}134 }135 guard := os.join_path(base, 'key.lock')136 old := time.now().unix() - 120137 138 os.write_file(guard, '${os.getpid()}')!139 os.utime(guard, old, old)!140 clear_stale_lock(guard)141 assert os.exists(guard)142 143 os.write_file(guard, '2000000')!144 os.utime(guard, old, old)!145 clear_stale_lock(guard)146 assert !os.exists(guard)147}