def assert_result_changes(func, args): """ Verifies that the result of the given function changes with time """ octopus.create_stream('stream0', x='int', y='text', z='int') name = 'assert_%s_decreases' % func octopus.create_cv( name, "SELECT %s(%s) FROM stream0 WHERE arrival_timestamp > clock_timestamp() - interval '2 seconds'" % (func, args)) rows = [(n, str(n), n + 1) for n in range(1000)] octopus.insert('stream0', ('x', 'y', 'z'), rows) current = 1 results = [] while current: row = octopus.execute('SELECT * FROM %s' % name).first() current = row[func] if current is None: break results.append(current) # Verify that we actually read something assert results octopus.drop_cv(name)
def test_output_tree(octopus, clean_db): """ Create a relatively complex tree of continuous views and transforms chained together by their output streams, and verify that all output correctly propagates to the leaves. """ octopus.create_stream('root', x='int') octopus.create_cv('level0_0', 'SELECT x::integer, count(*) FROM root GROUP BY x') octopus.create_cv('level1_0', 'SELECT (new).x, (new).count FROM level0_0_osrel') octopus.create_cv('level1_1', 'SELECT (new).x, (new).count FROM level0_0_osrel') octopus.create_cv('level2_0', 'SELECT (new).x, (new).count FROM level1_0_osrel') octopus.create_cv('level2_1', 'SELECT (new).x, (new).count FROM level1_0_osrel') octopus.create_cv('level2_2', 'SELECT (new).x, (new).count FROM level1_1_osrel') octopus.create_cv('level2_3', 'SELECT (new).x, (new).count FROM level1_1_osrel') octopus.create_cv('level3_0', 'SELECT (new).x, (new).count FROM level2_0_osrel') octopus.create_cv('level3_1', 'SELECT (new).x, (new).count FROM level2_0_osrel') octopus.create_cv('level3_2', 'SELECT (new).x, (new).count FROM level2_1_osrel') octopus.create_cv('level3_3', 'SELECT (new).x, (new).count FROM level2_1_osrel') octopus.create_cv('level3_4', 'SELECT (new).x, (new).count FROM level2_2_osrel') octopus.create_cv('level3_5', 'SELECT (new).x, (new).count FROM level2_2_osrel') octopus.create_cv('level3_6', 'SELECT (new).x, (new).count FROM level2_3_osrel') octopus.create_cv('level3_7', 'SELECT (new).x, (new).count FROM level2_3_osrel') octopus.insert('root', ('x',), [(x % 100,) for x in range(10000)]) names = [r[0] for r in octopus.execute('SELECT name FROM octopus_views() ORDER BY name DESC')] assert len(names) == 15 # Verify all values propagated to each node in the tree for name in names: rows = octopus.execute('SELECT x, max(count) FROM %s GROUP BY x' % name) for row in rows: x, count = row assert count == 100 octopus.insert('root', ('x',), [(x % 100,) for x in range(10000)]) # Verify all values propagated to each node in the tree again for name in names: rows = octopus.execute('SELECT x, max(count) FROM %s GROUP BY x' % name) for row in rows: x, count = row assert count == 200 # Drop these in reverse dependency order to prevent deadlocks for name in names: octopus.drop_cv(name)
def test_concurrent_sw_ticking(octopus, clean_db): """ Verify that several concurrent sliding-window queries each having different windows tick correctly at different intervals. """ octopus.create_stream('stream0', x='int') output_names = [] for n in range(10): name = 'sw%d' % n octopus.create_cv(name, 'SELECT x::integer, count(*) FROM stream0 GROUP BY x', sw='%d seconds' % (n + 10)) output_name = name + '_output' q = """ SELECT arrival_timestamp, CASE WHEN (old).x IS NULL THEN (new).x ELSE (old).x END AS x, old, new FROM %s_osrel """ % name octopus.create_cv(output_name, q) output_names.append(output_name) names = [r[0] for r in octopus.execute('SELECT name FROM octopus_views() ORDER BY name DESC')] assert len(names) == 2 * 10 octopus.insert('stream0', ('x',), [(x % 100,) for x in range(10000)]) time.sleep(25) for name in output_names: rows = list(octopus.execute('SELECT COUNT(DISTINCT x) FROM %s' % name)) assert rows[0][0] == 100 for x in range(100): # In window assert octopus.execute('SELECT * FROM %s WHERE old IS NULL AND new IS NOT NULL AND x = %d' % (name, x)) # Out of window assert octopus.execute('SELECT * FROM %s WHERE old IS NOT NULL AND new IS NULL AND x = %d' % (name, x)) # Drop these in reverse dependency order to prevent deadlocks for name in names: octopus.drop_cv(name)
def test_create_drop_continuous_view(octopus, clean_db): """ Basic sanity check """ octopus.create_stream('stream0', id='int') octopus.create_cv('cv0', 'SELECT id::integer FROM stream0') octopus.create_cv('cv1', 'SELECT id::integer FROM stream0') octopus.create_cv('cv2', 'SELECT id::integer FROM stream0') result = octopus.execute('SELECT * FROM octopus_views()') names = [r['name'] for r in result] assert sorted(names) == ['cv0', 'cv1', 'cv2'] octopus.drop_cv('cv0') octopus.drop_cv('cv1') octopus.drop_cv('cv2') result = octopus.execute('SELECT * FROM octopus_views()') names = [r['name'] for r in result] assert len(names) == 0
def test_create_views(octopus, clean_db): cvs = [] octopus.create_stream('stream0', x='int') q = 'SELECT count(*) FROM stream0' for i in xrange(1, MAX_CQS): cvs.append('cv_%d' % i) octopus.create_cv(cvs[-1], q) try: octopus.create_cv('cv_fail', q) assert False except Exception, e: assert 'maximum number of continuous queries exceeded' in e.message ids = [r['id'] for r in octopus.execute('SELECT id FROM octopus_views()')] assert len(set(ids)) == len(ids) assert set(ids) == set(xrange(1, MAX_CQS)) num_remove = random.randint(128, 512) for _ in xrange(num_remove): octopus.drop_cv(cvs.pop()) for _ in xrange(num_remove): cvs.append('cv_%d' % (len(cvs) + 1)) octopus.create_cv(cvs[-1], q)