server: fix flaky test_check_events_statuses test

PR-URL: https://github.com/hasura/graphql-engine-mono/pull/2467
Co-authored-by: paritosh-08 <85472423+paritosh-08@users.noreply.github.com>
GitOrigin-RevId: a6388397dae11e38962321915c06496cc57ffd64
This commit is contained in:
Sameer Kolhar 2021-10-22 07:54:59 +02:00 committed by hasura-bot
parent 459a7adbfb
commit 631f39cadf
2 changed files with 74 additions and 85 deletions

View File

@ -0,0 +1,3 @@
type: run_sql
args:
sql : delete from hdb_catalog.hdb_scheduled_events

View File

@ -7,6 +7,7 @@ from validate import validate_event_webhook,validate_event_headers
from queue import Empty from queue import Empty
import json import json
import time import time
from utils import until_asserts_pass
# The create and delete tests should ideally go in setup and teardown YAML files, # The create and delete tests should ideally go in setup and teardown YAML files,
# We can't use that here because, the payload is dynamic i.e. in case of one-off scheduled events # We can't use that here because, the payload is dynamic i.e. in case of one-off scheduled events
@ -15,10 +16,13 @@ import time
def stringify_datetime(dt): def stringify_datetime(dt):
return dt.strftime("%Y-%m-%dT%H:%M:%S.%fZ") return dt.strftime("%Y-%m-%dT%H:%M:%S.%fZ")
@pytest.mark.usefixtures('per_method_tests_db_state')
@pytest.mark.skip(msg="See https://github.com/hasura/graphql-engine-mono/pull/2467 for the details")
class TestScheduledEvent(object): class TestScheduledEvent(object):
@classmethod
def dir(cls):
return 'queries/scheduled_triggers'
webhook_payload = {"foo":"baz"} webhook_payload = {"foo":"baz"}
header_conf = [ header_conf = [
@ -28,68 +32,58 @@ class TestScheduledEvent(object):
} }
] ]
url = "/v1/query"
webhook_domain = "http://127.0.0.1:5594" webhook_domain = "http://127.0.0.1:5594"
def test_scheduled_events(self,hge_ctx,scheduled_triggers_evts_webhook):
# Succeeds
def test_create_scheduled_event(self,hge_ctx):
query = { query = {
"type":"create_scheduled_event", "type": "bulk",
"args":{ "args": [
"webhook":'{{SCHEDULED_TRIGGERS_WEBHOOK_DOMAIN}}/test', # Succeeds
"schedule_at":stringify_datetime(datetime.utcnow()), {
"payload":self.webhook_payload, "type":"create_scheduled_event",
"headers":self.header_conf, "args":{
"comment":"test scheduled event" "webhook":'{{SCHEDULED_TRIGGERS_WEBHOOK_DOMAIN}}/test',
} "schedule_at":stringify_datetime(datetime.utcnow()),
} "payload":self.webhook_payload,
st, resp = hge_ctx.v1q(query) "headers":self.header_conf,
assert st == 200,resp "comment":"test scheduled event"
# ensuring that a valid event_id is returned }
assert 'event_id' in resp, resp },
# Fails immediately, with 'dead'
# Fails immediately, with 'dead' {
def test_create_scheduled_event_with_very_old_scheduled_time(self,hge_ctx): "type":"create_scheduled_event",
query = { "args":{
"type":"create_scheduled_event", "webhook":"{{SCHEDULED_TRIGGERS_WEBHOOK_DOMAIN}}/",
"args":{ "schedule_at": "2020-01-01T00:00:00Z",
"webhook":"{{SCHEDULED_TRIGGERS_WEBHOOK_DOMAIN}}/", "payload":self.webhook_payload,
"schedule_at": "2020-01-01T00:00:00Z", "headers":self.header_conf
"payload":self.webhook_payload, }
"headers":self.header_conf },
} # Fails on request, trying twice:
} {
st, resp = hge_ctx.v1q(query) "type":"create_scheduled_event",
assert st == 200,resp "args":{
# ensuring that a valid event_id is returned "webhook":self.webhook_domain + '/fail',
assert 'event_id' in resp, resp "schedule_at": stringify_datetime(datetime.utcnow()),
"payload":self.webhook_payload,
# Fails on request, trying twice: "headers":self.header_conf,
def test_create_trigger_with_error_returning_webhook(self,hge_ctx): "retry_conf":{
query = { "num_retries":1,
"type":"create_scheduled_event", "retry_interval_seconds":1,
"args":{ "timeout_seconds":1,
"webhook":self.webhook_domain + '/fail', "tolerance_seconds": 21600
"schedule_at": stringify_datetime(datetime.utcnow()), }
"payload":self.webhook_payload, }
"headers":self.header_conf,
"retry_conf":{
"num_retries":1,
"retry_interval_seconds":1,
"timeout_seconds":1,
"tolerance_seconds": 21600
} }
} ]
} }
st, resp = hge_ctx.v1q(query) st, resp = hge_ctx.v1q(query)
assert st == 200, resp assert st == 200,resp
# ensuring that a valid event_id is returned assert len(resp) == 3, resp
assert 'event_id' in resp, resp # ensuring that valid event_id is returned for all requests
assert all(['event_id' in r for r in resp]), resp
# Here we check the three requests received by the webhook, from above. # Here we check the three requests received by the webhook.
def test_check_fired_webhook_events(self,hge_ctx,scheduled_triggers_evts_webhook):
# Collect the three generated events (they may arrive out of order): # Collect the three generated events (they may arrive out of order):
e1 = scheduled_triggers_evts_webhook.get_event(12) # at least 10 sec, see processScheduledTriggers.sleep e1 = scheduled_triggers_evts_webhook.get_event(12) # at least 10 sec, see processScheduledTriggers.sleep
e2 = scheduled_triggers_evts_webhook.get_event(12) e2 = scheduled_triggers_evts_webhook.get_event(12)
@ -121,36 +115,28 @@ class TestScheduledEvent(object):
assert k in payload_keys assert k in payload_keys
assert scheduled_triggers_evts_webhook.is_queue_empty() assert scheduled_triggers_evts_webhook.is_queue_empty()
def test_check_events_statuses(self,hge_ctx): def try_check_events_statuses():
query = { query = {
"type":"run_sql", "type":"run_sql",
"args":{ "args":{
"sql":"select status,tries from hdb_catalog.hdb_scheduled_events" "sql":"select status,tries from hdb_catalog.hdb_scheduled_events order by status desc"
}
}
st, resp = hge_ctx.v1q(query)
assert st == 200, resp
scheduled_event_statuses = dict(resp['result'])
# 3 scheduled events have been created
# one should be dead because the timestamp was past the tolerance limit
# one should be delivered because all the parameters were reasonable
# one should be error because the webhook returns an error state
assert scheduled_event_statuses == {
'status': 'tries',
'dead': '0',
'delivered': '1',
'error': '2' # num_retries + 1
} }
def test_teardown_scheduled_events(self,hge_ctx):
query = {
"type":"run_sql",
"args": {
"sql":"delete from hdb_catalog.hdb_scheduled_events"
} }
} st, resp = hge_ctx.v1q(query)
st, resp = hge_ctx.v1q(query) assert st == 200, resp
assert st == 200,resp scheduled_event_statuses = resp['result']
# 3 scheduled events have been created
# one should be dead because the timestamp was past the tolerance limit
# one should be delivered because all the parameters were reasonable
# one should be error because the webhook returns an error state
assert scheduled_event_statuses == [
['status', 'tries'],
['error', '2'], # num_retries + 1
['delivered', '1'],
['dead', '0']
], resp
until_asserts_pass(100,try_check_events_statuses)
class TestCronTrigger(object): class TestCronTrigger(object):