Skip to content

Commit

Permalink
Fix bug with claiming offer
Browse files Browse the repository at this point in the history
If an offer is claimed for its entire duration, and someone else
tries to claim the offer without specifying a start/end time, then
the db function will think that the next available end time is
before the current time. This fixes that issue, as well as renaming
the db function to be clearer regarding its function.
  • Loading branch information
tzumainn committed Feb 13, 2024
1 parent 7789605 commit 316a089
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 13 deletions.
3 changes: 2 additions & 1 deletion esi_leap/api/controllers/v1/offer.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,8 @@ def claim(self, offer_uuid, new_lease):
lease_dict['start_time'] = datetime.datetime.now()

if 'end_time' not in lease_dict:
q = offer.get_first_availability(lease_dict['start_time'])
q = offer.get_next_lease_start_time(
lease_dict['start_time'])
if q is None:
lease_dict['end_time'] = offer.end_time
else:
Expand Down
6 changes: 3 additions & 3 deletions esi_leap/db/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ def offer_get_conflict_times(offer_ref):
return IMPL.offer_get_conflict_times(offer_ref)


def offer_get_first_availability(offer_uuid, start, end):
return IMPL.offer_get_first_availability(
offer_uuid, start, end)
def offer_get_next_lease_start_time(offer_uuid, start):
return IMPL.offer_get_next_lease_start_time(
offer_uuid, start)


def offer_verify_availability(offer_ref, start, end):
Expand Down
5 changes: 3 additions & 2 deletions esi_leap/db/sqlalchemy/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def offer_get_conflict_times(offer_ref):
).all()


def offer_get_first_availability(offer_uuid, start):
def offer_get_next_lease_start_time(offer_uuid, start):
l_query = model_query(models.Lease)

return l_query.with_entities(
Expand All @@ -179,7 +179,8 @@ def offer_get_first_availability(offer_uuid, start):
(models.Lease.status == statuses.ACTIVE)
).\
order_by(models.Lease.start_time).\
filter(models.Lease.end_time >= start).first()
filter((models.Lease.end_time >= start) &
(models.Lease.start_time >= start)).first()


def offer_verify_availability(offer_ref, start, end):
Expand Down
5 changes: 3 additions & 2 deletions esi_leap/objects/offer.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,9 @@ def get_availabilities(self):

return avails

def get_first_availability(self, start):
return self.dbapi.offer_get_first_availability(self.uuid, start)
def get_next_lease_start_time(self, start):
return self.dbapi.offer_get_next_lease_start_time(
self.uuid, start)

def create(self, context=None):
updates = self.obj_get_changes()
Expand Down
27 changes: 22 additions & 5 deletions esi_leap/tests/db/sqlalchemy/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,20 @@
status=statuses.EXPIRED,
)

test_lease_7 = dict(
uuid='77777',
project_id='1e5533_2',
owner_id='0wn3r_2',
name='l1',
resource_uuid='1111',
resource_type='dummy_node',
purpose='test_purpose',
start_time=now - datetime.timedelta(days=10),
end_time=now + datetime.timedelta(days=10),
properties={},
status=statuses.ACTIVE,
)

test_event_1 = dict(
id=1,
event_type='fake:event:start',
Expand Down Expand Up @@ -340,15 +354,18 @@ def test_offer_get_conflict_times(self):
[(now + datetime.timedelta(days=50),
now + datetime.timedelta(days=60))])

def test_offer_get_first_availability(self):
def test_offer_get_next_lease_start_time(self):
o1 = api.offer_create(test_offer_1)
self.assertEqual(api.offer_get_first_availability
self.assertEqual(api.offer_get_next_lease_start_time
(o1.uuid, o1.start_time,), None)
test_lease_3['offer_uuid'] = o1.uuid
api.lease_create(test_lease_3)
self.assertEqual(api.offer_get_first_availability(o1.uuid,
o1.start_time),
(now + datetime.timedelta(days=50),))
test_lease_7['offer_uuid'] = o1.uuid
api.lease_create(test_lease_7)
self.assertEqual(
api.offer_get_next_lease_start_time(
o1.uuid, o1.start_time),
(now + datetime.timedelta(days=50),))

def test_offer_get_by_uuid(self):
o1 = api.offer_create(test_offer_1)
Expand Down

0 comments on commit 316a089

Please sign in to comment.