示例#1
0
    def test_remaining(self):
        q = Quota(1)
        self.assertEquals(1, q.remaining())

        q._start()
        self.assertTrue(q.running())
        time.sleep(0.5)
        q._stop()

        self.assertFalse(q.running())
        self.assertLessEqual(q.remaining(), 0.5)
示例#2
0
    def test_continuation(self):
        q = Quota(1)

        q._start()
        q._stop()
        time.sleep(0.2)
        q._start()
        q._stop()

        # tolerate a little time spent outside the sleep:
        self.assertGreater(q.remaining(), 0.9)
示例#3
0
    def test_remaining(self):
        q = Quota(1)
        self.assertEquals(1, q.remaining())

        q._start()
        self.assertTrue(q.running())
        time.sleep(0.5)
        q._stop()

        self.assertFalse(q.running())
        self.assertLessEqual(q.remaining(), 0.5)
示例#4
0
def calc_ged(recepie1, recepie2, timeout_val=600):
    start_time = time.time()
    G1 = make_graph(recepie1)
    G2 = make_graph(recepie2)
    ged = None

    try:
        status = "OK"
        with timeout(Quota(timeout_val), exception=RuntimeError):
            for ged in nx.optimize_graph_edit_distance(G1, G2, lambda n1, n2: n1['op'] == n2['op']):
                pass

    except RuntimeError as e:
        status = "Timeout"

    except Exception as e:
        status = "Exception: " + str(e)

    return {
        "recepie_i": recepie1,
        "recepie_j": recepie2,
        "ged": ged,
        "time": time.time() - start_time,
        "status": status
    }
示例#5
0
    def test_timeout(self):
        q = Quota(1)
        with timeout(q, RuntimeError):
            time.sleep(0.2)
        self.assertTrue(0.75 < q.remaining() <= 0.8)
        time.sleep(1)
        self.assertTrue(0.75 < q.remaining() <= 0.8)
        with timeout(q, RuntimeError):
            time.sleep(0.2)
        self.assertTrue(0.55 < q.remaining() <= 0.6)

        try:
            with timeout(q, RuntimeError):
                time.sleep(0.7)
            self.fail()
        except RuntimeError:
            pass
示例#6
0
    def test_timeout(self):
        q = Quota(1)
        with timeout(q, RuntimeError):
            time.sleep(0.2)
        self.assertTrue(0.75 < q.remaining() <= 0.8)
        time.sleep(1)
        self.assertTrue(0.75 < q.remaining() <= 0.8)
        with timeout(q, RuntimeError):
            time.sleep(0.2)
        self.assertTrue(0.55 < q.remaining() <= 0.6)

        try:
            with timeout(q, RuntimeError):
                time.sleep(0.7)
            self.fail()
        except RuntimeError:
            pass
示例#7
0
    def test_nesting(self):
        q1 = Quota(0.5)
        q2 = Quota(1)

        try:
            with timeout(q1, Outer):
                with timeout(q2, Inner):
                    time.sleep(1)
            self.fail()
        except Outer:
            self.assertFalse(q1.running())
            self.assertLessEqual(q1.remaining(), 0.05)

            self.assertFalse(q2.running())
            self.assertTrue(0.45 < q2.remaining() <= 0.5)
示例#8
0
    def test_nesting(self):
        q1 = Quota(0.5)
        q2 = Quota(1)

        try:
            with timeout(q1, Outer):
                with timeout(q2, Inner):
                    time.sleep(1)
            self.fail()
        except Outer:
            self.assertFalse(q1.running())
            self.assertLessEqual(q1.remaining(), 0.05)

            self.assertFalse(q2.running())
            self.assertTrue(0.45 < q2.remaining() <= 0.5)
示例#9
0
    def test_continuation(self):
        q = Quota(1)

        q._start()
        q._stop()
        time.sleep(0.2)
        q._start()
        q._stop()

        # tolerate a little time spent outside the sleep:
        self.assertGreater(q.remaining(), 0.9)