<commit_before>import sys
import copy
import time
import mock
import random
import unittest
from rackattack.tcp import publish
from rackattack.common import timer
from rackattack.common import globallock
from rackattack.physical.alloc import allocation
from rackattack.physical.alloc.freepool import FreePool
from rackattack.physical.tests.common import (HostStateMachine, Host, Hosts, Publish,
                                              executeCodeWhileAllocationIsDeadOfHeartbeatTimeout)


class Test(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.broadcaster = Publish()

    def setUp(self):
        globallock._lock.acquire()
        self.currentTimer = None
        self.currentTimerTag = None
        timer.scheduleIn = self.scheduleTimerIn
        timer.cancelAllByTag = self.cancelAllTimersByTag
        requirements = dict(node0=dict(imageHint='alpha-bravo', imageLabel='tango-lima'),
                            node1=dict(imageHint='charlie-delta', imageLabel='kilo-juliet'),
                            node2=dict(imageHint='echo-foxtrot', imageLabel='zooloo-papa'))
        self.index = random.randint(1, sys.maxint)
        self.allocationInfo = 'This allocation has got swag.'
        allocated = dict((hostName, HostStateMachine(Host(hostName)))
                         for hostName in requirements.keys())
        self.originalAllocated = copy.copy(allocated)
        self.expectedStates = dict(allocatedButNotInaugurated=set(allocated.values()),
                                   inaugurated=set())
        self.expectedDestroyed = set()
        self.expectedDetached = set()
        self.expectedReleased = set()
        self.wasAllocationDoneAtSomePoint = False
        self.broadcaster.reset_mock()
        self.hosts = Hosts()
        self.freepool = FreePool(self.hosts)
        for stateMachine in allocated.values():
            self.hosts.add(stateMachine)
        self.tested = allocation.Allocation(self.index, requirements, self.allocationInfo, allocated,
                                            self.broadcaster, self.freepool, self.hosts)

    def tearDown(self):
        globallock._lock.release()

    def test_Index(self):
        self.assertEquals(self.tested.index(), self.index)

    def test_AllocationInfo(self):
        self.assertEquals(self.allocationInfo, self.tested.allocationInfo())

    def test_InauguratedWhenNotDoneRaisesAssertionError(self):
        self.assertRaises(AssertionError, self.tested.inaugurated)

    def test_InauguratedWhenDone(self):
        self.fakeInaugurationDoneForAll()
        expected = {stateMachine.hostImplementation().id(): stateMachine for stateMachine in
                    self.expectedStates["inaugurated"]}
        self.assertEquals(expected, self.tested.inaugurated())

    def test_Allocated(self):
        self.assertEquals(self.originalAllocated, self.tested.allocated())

    def test_Free(self):
        self.tested.free()
        self.assertEquals(self.tested.dead(), "freed")
        self.validate()

    def test_CantFreeDeadAllocation(self):
        self.tested.free()
        self.assertEquals(self.tested.dead(), "freed")
        self.assertRaises(Exception, self.tested.free)
        self.validate()

    def test_Withdraw(self):<commit_msg>Fix deadlock in UT when globallock is not released since tearDown is not run when the test fails<commit_after>import sys
import copy
import time
import mock
import random
import unittest
from rackattack.tcp import publish
from rackattack.common import timer
from rackattack.common import globallock
from rackattack.physical.alloc import allocation
from rackattack.physical.alloc.freepool import FreePool
from rackattack.physical.tests.common import (HostStateMachine, Host, Hosts, Publish,
                                              executeCodeWhileAllocationIsDeadOfHeartbeatTimeout)


class Test(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.broadcaster = Publish()

    def setUp(self):
        self.addCleanup(globallock._lock.release)
        globallock._lock.acquire()
        self.currentTimer = None
        self.currentTimerTag = None
        timer.scheduleIn = self.scheduleTimerIn
        timer.cancelAllByTag = self.cancelAllTimersByTag
        requirements = dict(node0=dict(imageHint='alpha-bravo', imageLabel='tango-lima'),
                            node1=dict(imageHint='charlie-delta', imageLabel='kilo-juliet'),
                            node2=dict(imageHint='echo-foxtrot', imageLabel='zooloo-papa'))
        self.index = random.randint(1, sys.maxint)
        self.allocationInfo = 'This allocation has got swag.'
        allocated = dict((hostName, HostStateMachine(Host(hostName)))
                         for hostName in requirements.keys())
        self.originalAllocated = copy.copy(allocated)
        self.expectedStates = dict(allocatedButNotInaugurated=set(allocated.values()),
                                   inaugurated=set())
        self.expectedDestroyed = set()
        self.expectedDetached = set()
        self.expectedReleased = set()
        self.wasAllocationDoneAtSomePoint = False
        self.broadcaster.reset_mock()
        self.hosts = Hosts()
        self.freepool = FreePool(self.hosts)
        for stateMachine in allocated.values():
            self.hosts.add(stateMachine)
        self.tested = allocation.Allocation(self.index, requirements, self.allocationInfo, allocated,
                                            self.broadcaster, self.freepool, self.hosts)

    def test_Index(self):
        self.assertEquals(self.tested.index(), self.index)

    def test_AllocationInfo(self):
        self.assertEquals(self.allocationInfo, self.tested.allocationInfo())

    def test_InauguratedWhenNotDoneRaisesAssertionError(self):
        self.assertRaises(AssertionError, self.tested.inaugurated)

    def test_InauguratedWhenDone(self):
        self.fakeInaugurationDoneForAll()
        expected = {stateMachine.hostImplementation().id(): stateMachine for stateMachine in
                    self.expectedStates["inaugurated"]}
        self.assertEquals(expected, self.tested.inaugurated())

    def test_Allocated(self):
        self.assertEquals(self.originalAllocated, self.tested.allocated())

    def test_Free(self):
        self.tested.free()
        self.assertEquals(self.tested.dead(), "freed")
        self.validate()

    def test_CantFreeDeadAllocation(self):
        self.tested.free()
        self.assertEquals(self.tested.dead(), "freed")
        self.assertRaises(Exception, self.tested.free)
        self.validate()

    def test_Withdraw(self):
{'fraction_non_alphanumeric': 0.09479245283018868, 'fraction_numerical': 0.0012075471698113208, 'mean_word_length': 2.834490740740741, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4929551', 'n_tokens_mistral': 1848, 'n_tokens_neox': 1773, 'n_words': 332}