File: //home/arjun/projects/buyercall_forms/buyercall/buyercall/tests/bandwidth/test_calls.py
# import time
# import logging
# from multiprocessing import Process
# from threading import Thread
#
# import pytest
# from flask import request
#
# from buyercall.blueprints.leads.models import Lead
# from buyercall.blueprints.phonenumbers.models import Phone
# from pantwidth.server import start_testserver, Actor
#
#
# logging.basicConfig(format='%(asctime)s - %(levelname)s: %(message)s')
#
# log = logging.getLogger('buyercall.test')
#
#
# def start_app(app):
# app.run(threaded=True)
#
#
# def wait_until(condition, timeout=5):
# while True:
# if condition():
# return True
# if timeout <= 0:
# return False
# time.sleep(1)
# timeout -= 1
#
#
# class TestCalls(object):
# def test_missed_call(self, app, db, client, numbers):
# try:
# t = Process(target=start_app, args=(app,))
# t.start()
#
# number_count = db.session.query(Phone).count()
# assert number_count > 0
#
# with start_testserver(
# actors=[DummyActor('+12345552233')],
# users={
# 'u-hgygmfipmuw7asnskzybr4y': []
# },
# applications=[{
# 'numbers': ['+11115551111'],
# 'url': 'http://buyercall.local:8000/api/bw/inbound/1',
# }],
# ):
# assert wait_until(
# lambda: db.session.query(Lead).filter(Lead.status == 'missed').
# count() > 0
# )
# finally:
# t.terminate()
# t.join()
#
# def test_regular_call(self, app, db, client, numbers):
# try:
# t = Process(target=start_app, args=(app,))
# t.start()
#
# with start_testserver(
# actors=[SmartActor('+12345552233')],
# users={
# 'u-hgygmfipmuw7asnskzybr4y': []
# },
# applications=[{
# 'numbers': ['+11115551111'],
# 'url': 'http://buyercall.local:8000/api/bw/inbound/1',
# }],
# ):
# assert wait_until(
# lambda: db.session.query(Lead).filter(Lead.status == 'active').
# count() > 0
# )
# finally:
# t.terminate()
# t.join()
#
#
# class DummyActor(Actor):
# def handle_event(self, event, **event_args):
# log.info('Actor got event: {}'.format(event))
# if event == 'start':
# return 'call', {'to': '+11115551111'}
# if event == 'call_event' and event_args['callState'] == 'active':
# time.sleep(3)
# return 'hangup', None
#
#
# class SmartActor(Actor):
# def handle_event(self, event, **event_args):
# log.info('Actor got event: {}'.format(event))
# if event == 'start':
# return 'call', {'to': '+11115551111'}
# return None, None