#!/usr/bin/env qore
# -*- mode: qore; indent-tabs-mode: nil -*-

%new-style
%enable-all-warnings
%require-types
%strict-args

%requires ../../../../qlib/QUnit.qm

%exec-class SetThreadInit


public class SetThreadInit inherits QUnit::Test {
    constructor() : Test("set_thread_init", "1.0") {
        addTestCase("set_thread_init", \setThreadInitTest());

        # Return for compatibility with test harness that checks return value
        set_return_value(main());
    }

    setThreadInitTest() {
        our int i = 0;
        our Counter cnt(1);
        bool b = set_thread_init(sub () { i = 1; });
        assertEq(False, b);
        background cnt.dec();
        cnt.waitForZero();
        assertEq(1, i);
        i = 0;
        b = set_thread_init();
        assertEq(True, b);
        cnt.inc();
        background cnt.dec();
        cnt.waitForZero();
        assertEq(0, i);
    }
}
