| 1 | module nspkg |
| 2 | |
| 3 | import os |
| 4 | |
| 5 | #include "@VMODROOT/tests/log_probe.h" |
| 6 | |
| 7 | fn C.v_alpm_log_probe(CbLog, voidptr, LogLevel, &u8, i32) |
| 8 | |
| 9 | struct LogLineTrace { |
| 10 | mut: |
| 11 | messages []string |
| 12 | levels []LogLevel |
| 13 | handle &Handle = unsafe { nil } |
| 14 | replacement &LogLineTrace = unsafe { nil } |
| 15 | } |
| 16 | |
| 17 | fn record_log_line(context voidptr, level LogLevel, text &u8) { |
| 18 | mut state := unsafe { &LogLineTrace(context) } |
| 19 | state.messages << unsafe { cstring_to_vstring(text) } |
| 20 | state.levels << level |
| 21 | } |
| 22 | |
| 23 | fn replace_log_line(context voidptr, level LogLevel, text &u8) { |
| 24 | mut state := unsafe { &LogLineTrace(context) } |
| 25 | record_log_line(context, level, text) |
| 26 | state.handle.option_set_logcb_line(record_log_line, state.replacement) or { assert false, err.msg() } |
| 27 | } |
| 28 | |
| 29 | fn record_raw_log(context voidptr, level LogLevel, format &u8, _ voidptr) { |
| 30 | record_log_line(context, level, format) |
| 31 | } |
| 32 | |
| 33 | fn test_log_line_callback_formats_full_messages_and_preserves_raw_callback_api() ! { |
| 34 | base := os.join_path(os.vtmp_dir(), 'alpm-log-lines-${os.getpid()}') |
| 35 | os.mkdir_all(base)! |
| 36 | defer { os.rmdir_all(base) or { eprintln(err) } } |
| 37 | handle := initialize('/', base)! |
| 38 | defer { handle.release() or { eprintln(err) } } |
| 39 | assert voidptr(handle.option_get_logcb_line()) == unsafe { nil } |
| 40 | assert handle.option_get_logcb_line_ctx() == unsafe { nil } |
| 41 | mut state := &LogLineTrace{} |
| 42 | handle.option_set_logcb_line(record_log_line, state)! |
| 43 | assert voidptr(handle.option_get_logcb_line()) == voidptr(record_log_line) |
| 44 | assert handle.option_get_logcb_line_ctx() == voidptr(state) |
| 45 | C.v_alpm_log_probe(handle.option_get_logcb(), handle.option_get_logcb_ctx(), .warning, c'café λ', 12) |
| 46 | assert state.messages == ['café λ:12\n'] |
| 47 | assert state.levels == [LogLevel.warning] |
| 48 | long_message := 'data'.repeat(2048) |
| 49 | C.v_alpm_log_probe(handle.option_get_logcb(), handle.option_get_logcb_ctx(), .debug, long_message.str, -8) |
| 50 | assert state.messages.last() == long_message + ':-8\n' |
| 51 | // resetting the current raw pair must not free the active line adapter's state. |
| 52 | handle.option_set_logcb(handle.option_get_logcb(), handle.option_get_logcb_ctx())! |
| 53 | C.v_alpm_log_probe(handle.option_get_logcb(), handle.option_get_logcb_ctx(), .error, c'after-reset', 3) |
| 54 | assert state.messages.last() == 'after-reset:3\n' |
| 55 | handle.option_set_logcb(record_raw_log, state)! |
| 56 | assert voidptr(handle.option_get_logcb_line()) == unsafe { nil } |
| 57 | assert handle.option_get_logcb_line_ctx() == unsafe { nil } |
| 58 | assert handle.option_get_logcb_ctx() == voidptr(state) |
| 59 | C.v_alpm_log_probe(handle.option_get_logcb(), handle.option_get_logcb_ctx(), .error, c'raw', 7) |
| 60 | assert state.messages.last() == '%s:%d\n' |
| 61 | handle.option_set_logcb_line(record_log_line, state)! |
| 62 | handle.option_set_logcb_line(unsafe { nil }, unsafe { nil })! |
| 63 | assert voidptr(handle.option_get_logcb()) == unsafe { nil } |
| 64 | assert voidptr(handle.option_get_logcb_line()) == unsafe { nil } |
| 65 | } |
| 66 | |
| 67 | fn test_log_line_callback_can_replace_itself_during_delivery() ! { |
| 68 | base := os.join_path(os.vtmp_dir(), 'alpm-log-replace-${os.getpid()}') |
| 69 | os.mkdir_all(base)! |
| 70 | defer { os.rmdir_all(base) or { eprintln(err) } } |
| 71 | handle := initialize('/', base)! |
| 72 | defer { handle.release() or { eprintln(err) } } |
| 73 | mut second := &LogLineTrace{} |
| 74 | mut first := &LogLineTrace{ handle: handle, replacement: second } |
| 75 | handle.option_set_logcb_line(replace_log_line, first)! |
| 76 | C.v_alpm_log_probe(handle.option_get_logcb(), handle.option_get_logcb_ctx(), .warning, c'first', 1) |
| 77 | C.v_alpm_log_probe(handle.option_get_logcb(), handle.option_get_logcb_ctx(), .error, c'second', 2) |
| 78 | assert first.messages == ['first:1\n'] |
| 79 | assert second.messages == ['second:2\n'] |
| 80 | assert handle.option_get_logcb_line_ctx() == voidptr(second) |
| 81 | } |
| 82 | |
| 83 | fn handle_with_only_a_log_line_context(path string) !&Handle { |
| 84 | handle := initialize('/', path)! |
| 85 | handle.option_set_logcb_line(record_log_line, &LogLineTrace{})! |
| 86 | return handle |
| 87 | } |
| 88 | |
| 89 | fn test_log_line_context_in_its_libc_adapter_remains_reachable_by_the_collector() ! { |
| 90 | base := os.join_path(os.vtmp_dir(), 'alpm-log-line-gc-${os.getpid()}') |
| 91 | os.mkdir_all(base)! |
| 92 | defer { os.rmdir_all(base) or { eprintln(err) } } |
| 93 | handle := handle_with_only_a_log_line_context(base)! |
| 94 | defer { handle.release() or { eprintln(err) } } |
| 95 | for index in 0 .. 8 { |
| 96 | gc_collect() |
| 97 | C.v_alpm_log_probe(handle.option_get_logcb(), handle.option_get_logcb_ctx(), .warning, c'retained', i32(index)) |
| 98 | } |
| 99 | state := unsafe { &LogLineTrace(handle.option_get_logcb_line_ctx()) } |
| 100 | assert state.messages == ['retained:0\n', 'retained:1\n', 'retained:2\n', 'retained:3\n', |
| 101 | 'retained:4\n', 'retained:5\n', 'retained:6\n', 'retained:7\n'] |
| 102 | handle.option_set_logcb_line(unsafe { nil }, unsafe { nil })! |
| 103 | assert handle.option_get_logcb_line_ctx() == unsafe { nil } |
| 104 | } |
| 105 | |