|
|
@@ -26,10 +26,14 @@ |
|
|
|
import aiohttp |
|
|
|
import asyncio |
|
|
|
import concurrent |
|
|
|
import logging |
|
|
|
import os |
|
|
|
import shutil |
|
|
|
import tempfile |
|
|
|
import unittest |
|
|
|
import urllib |
|
|
|
|
|
|
|
logging.basicConfig(level=logging.DEBUG) |
|
|
|
|
|
|
|
from aiohttp import web |
|
|
|
from RainEagle.parse import LogDir as RELogDir, _cmaiter |
|
|
@@ -73,8 +77,21 @@ class SolarDataWS(object): |
|
|
|
# XXX - how to configure this properly |
|
|
|
sdws = SolarDataWS('./raineagle.') |
|
|
|
|
|
|
|
app = web.Application() |
|
|
|
app.add_routes(sdws.get_routes()) |
|
|
|
def getapp(sdws): |
|
|
|
app = web.Application() |
|
|
|
app.add_routes(sdws.get_routes()) |
|
|
|
p = os.path.join(os.path.dirname(__file__), '..', 'root') |
|
|
|
# XXX - aiohttp doesn't have a way to serve static files, or do |
|
|
|
# a simple redirect |
|
|
|
async def index(request): |
|
|
|
return web.FileResponse(os.path.join(p, 'index.html')) |
|
|
|
|
|
|
|
app.add_routes([ web.RouteDef('GET', '/', index, kwargs={}) ]) |
|
|
|
app.add_routes([ web.static(prefix='/', path=p) ]) |
|
|
|
|
|
|
|
return app |
|
|
|
|
|
|
|
app = getapp(sdws) |
|
|
|
|
|
|
|
# https://stackoverflow.com/questions/23033939/how-to-test-python-3-4-asyncio-code |
|
|
|
# Slightly modified to timeout and to print trace back when canceled. |
|
|
@@ -105,27 +122,47 @@ class Test(unittest.TestCase): |
|
|
|
self.tempdir = os.path.join(d, 'subdir') |
|
|
|
os.mkdir(self.tempdir) |
|
|
|
|
|
|
|
self.rootdir = os.path.join(os.path.dirname(__file__), '..', 'root') |
|
|
|
os.chdir(self.tempdir) |
|
|
|
sdws = SolarDataWS(os.path.join(self.tempdir, 'raineagle')) |
|
|
|
|
|
|
|
app = web.Application() |
|
|
|
app.add_routes(sdws.get_routes()) |
|
|
|
app = getapp(sdws) |
|
|
|
self._app = app |
|
|
|
|
|
|
|
# launch the server |
|
|
|
self._webport = 58323 |
|
|
|
|
|
|
|
def makeurl(self, path): |
|
|
|
return urllib.parse.urljoin('http://localhost:%d/' % self._webport, |
|
|
|
path) |
|
|
|
|
|
|
|
def tearDown(self): |
|
|
|
#print('td:', time.time()) |
|
|
|
shutil.rmtree(self.basetempdir) |
|
|
|
self.tempdir = None |
|
|
|
|
|
|
|
@async_test |
|
|
|
async def test_staticfiles(self): |
|
|
|
runner = web.AppRunner(self._app) |
|
|
|
await runner.setup() |
|
|
|
site = web.TCPSite(runner, 'localhost', self._webport) |
|
|
|
await site.start() |
|
|
|
|
|
|
|
async with aiohttp.ClientSession() as session, \ |
|
|
|
session.get(self.makeurl('')) as req: |
|
|
|
#print(repr(req)) |
|
|
|
self.assertEqual(req.status, 200) |
|
|
|
body = await req.content.read() |
|
|
|
with open(os.path.join(self.rootdir, 'index.html'), 'rb') as fp: |
|
|
|
self.assertEqual(body, fp.read()) |
|
|
|
|
|
|
|
@async_test |
|
|
|
async def test_daemon(self): |
|
|
|
loop = asyncio.get_event_loop() |
|
|
|
|
|
|
|
# launch the server |
|
|
|
webport = 58323 |
|
|
|
runner = web.AppRunner(self._app) |
|
|
|
await runner.setup() |
|
|
|
site = web.TCPSite(runner, 'localhost', webport) |
|
|
|
site = web.TCPSite(runner, 'localhost', self._webport) |
|
|
|
await site.start() |
|
|
|
|
|
|
|
with open(os.path.join(self.tempdir, 'raineagle.0.log'), 'w', buffering=1) as fp: |
|
|
@@ -136,14 +173,13 @@ class Test(unittest.TestCase): |
|
|
|
r = [] |
|
|
|
try: |
|
|
|
async with aiohttp.ClientSession() as session, \ |
|
|
|
session.ws_connect('http://localhost:%d/ws' % |
|
|
|
webport) as ws: |
|
|
|
session.ws_connect(self.makeurl('ws')) as ws: |
|
|
|
async for msg in ws: |
|
|
|
r.append(msg) |
|
|
|
if len(r) == 2: |
|
|
|
break |
|
|
|
|
|
|
|
await ws.send_str('q') |
|
|
|
#await ws.send_str('q') |
|
|
|
|
|
|
|
self.assertEqual(r[0].type, aiohttp.WSMsgType.TEXT) |
|
|
|
self.assertEqual(r[0].data, 'ng 1.2740') |
|
|
|