folder_test.v

4.8 KB · raw

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	// A stale tag loses.47	assert !backend.put_if_match('repo/head', 'two'.bytes(), 'a-stale-tag')!48	assert backend.get('repo/head')!.body == 'one'.bytes()4950	// The current tag wins.51	assert backend.put_if_match('repo/head', 'two'.bytes(), tag)!52	assert backend.get('repo/head')!.body == 'two'.bytes()5354	// An absent key can only be created when the caller read it as absent.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	// A writer mid-publish, or one that died, leaves its own files behind.73	// They are not keys: a listing that returned them would send steal after74	// events and saves that do not exist.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	// Another machine setting up at the same moment has its own probe on the102	// remote; this check must neither trip over it nor delete it.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	// The lock uses the same primitive, and still holds only one writer.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.hostname()!} ${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	host := os.hostname()!138	// Our own pid is alive, so an old lock is still a writer's.139	os.write_file(guard, '${host} ${os.getpid()}')!140	os.utime(guard, old, old)!141	clear_stale_lock(guard)142	assert os.exists(guard)143	// A pid nothing holds is debris, and clears.144	os.write_file(guard, '${host} 2000000')!145	os.utime(guard, old, old)!146	clear_stale_lock(guard)147	assert !os.exists(guard)148	// Another machine's pid says nothing about this one's processes: age alone decides.149	os.write_file(guard, 'another-machine ${os.getpid()}')!150	os.utime(guard, old, old)!151	clear_stale_lock(guard)152	assert !os.exists(guard)153}