Repository: dogecoin/dogecoin
File: qa/rpc-tests/getmocktime.py (Python)

Code before:

    def create_testnode(self, node_idx=0):
        node = GetMockTimeTestNode()
        conn = NodeConn('127.0.0.1', p2p_port(node_idx), self.nodes[node_idx], node)
        node.add_connection(conn)
        return node

    def test_getmocktime(self):
        assert wait_until_mocktime(self.nodes[0], 0, timeout=60)

        self.nodes[0].setmocktime(-0) # negative zero
        assert wait_until_mocktime(self.nodes[0], -0, timeout=60)

        self.nodes[0].setmocktime(123)
        assert wait_until_mocktime(self.nodes[0], 123, timeout=60)

        self.nodes[0].setmocktime(9223372036854775807) # int64_t max
        assert wait_until_mocktime(self.nodes[0], 9223372036854775807, timeout=60)

        try: # overflow int64_t max
            self.nodes[0].setmocktime(9223372036854775808) # int64_t max + 1
        except JSONRPCException as e:
            assert("JSON integer out of range" in e.error["message"])

        self.nodes[0].setmocktime(-9223372036854775808) # int64_t min
        assert wait_until_mocktime(self.nodes[0], -9223372036854775808, timeout=60)

        try: # overflow int64_t min
            self.nodes[0].setmocktime(-9223372036854775809) # int64_t min + 1
        except JSONRPCException as e:
            assert("JSON integer out of range" in e.error["message"])

        self.nodes[0].setmocktime(0x7fffffffffffffff) # hex
        assert wait_until_mocktime(self.nodes[0], 0x7fffffffffffffff, timeout=60)

        self.nodes[0].setmocktime(1658308113) # 4314284 timestamp
        assert not wait_until_mocktime_change(self.nodes[0], 1658308113, timeout=5) # this must time out
        assert wait_until_mocktime(self.nodes[0], 1658308113, timeout=60)

        self.nodes[0].setmocktime(1658308119) # 4314285 timestamp
        assert not wait_until_mocktime_change(self.nodes[0], 1658308119, timeout=5) # this must time out
        assert wait_until_mocktime(self.nodes[0], 1658308119, timeout=60)

if __name__ == '__main__':
    GetMockTime().main()


Review comment (bug):
these need to be flipped (also with the next test) because there is a race condition now:

1. You set mocktime, because this is an async operation you need to block the test until it is actually set to prevent race from the test. This is what the `wait_until_mocktime(<mocktime_we_just_set>)` call does.
2. Once it is set, you can check whether something changes it with `wait_until_mocktime_change`, and that should never happen.

Code after:
        assert wait_until_mocktime(self.nodes[0], -0, timeout=60)

        self.nodes[0].setmocktime(123)
        assert wait_until_mocktime(self.nodes[0], 123, timeout=60)

        self.nodes[0].setmocktime(9223372036854775807) # int64_t max
        assert wait_until_mocktime(self.nodes[0], 9223372036854775807, timeout=60)

        try: # overflow int64_t max
            self.nodes[0].setmocktime(9223372036854775808) # int64_t max + 1
        except JSONRPCException as e:
            assert("JSON integer out of range" in e.error["message"])

        self.nodes[0].setmocktime(-9223372036854775808) # int64_t min
        assert wait_until_mocktime(self.nodes[0], -9223372036854775808, timeout=60)

        try: # overflow int64_t min
            self.nodes[0].setmocktime(-9223372036854775809) # int64_t min + 1
        except JSONRPCException as e:
            assert("JSON integer out of range" in e.error["message"])

        self.nodes[0].setmocktime(0x7fffffffffffffff) # hex
        assert wait_until_mocktime(self.nodes[0], 0x7fffffffffffffff, timeout=60)

        self.nodes[0].setmocktime(1658308113) # 4314284 timestamp
        assert wait_until_mocktime(self.nodes[0], 1658308113, timeout=60)
        assert not wait_until_mocktime_change(self.nodes[0], 1658308113, timeout=5) # this must time out

        self.nodes[0].setmocktime(1658308119) # 4314285 timestamp
        assert wait_until_mocktime(self.nodes[0], 1658308119, timeout=60)
        assert not wait_until_mocktime_change(self.nodes[0], 1658308119, timeout=5) # this must time out

        self.nodes[0].setmocktime(0) # zero
        assert wait_until_mocktime(self.nodes[0], 0, timeout=60)

if __name__ == '__main__':
    GetMockTime().main()