Skip to content

Commit

Permalink
[#1355] test kill play
Browse files Browse the repository at this point in the history
  • Loading branch information
xael-fry committed Apr 3, 2022
1 parent 2f80845 commit 4454c8f
Showing 1 changed file with 72 additions and 73 deletions.
145 changes: 72 additions & 73 deletions samples-and-tests/i-am-a-developer/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@


# --- TESTS
DEFAULTS = {
'host': '127.0.0.1',
'http.port': '9001',
}

class IamADeveloper(unittest.TestCase):
DEFAULTS = {
'host': '127.0.0.1',
'http.port': '9001',

}
play = None

@unittest.skip
def testSSLConfig(self):
Expand All @@ -48,7 +49,7 @@ def testSSLConfig(self):
insert(app, "app/controllers/Application.java", 13, ' Logger.info("I am ssl secured!");')

edit(app, "conf/application.conf", 32, 'http.port=-1')
edit(app, "conf/application.conf", 33, 'https.port=' + self.DEFAULTS['http.port'])
edit(app, "conf/application.conf", 33, 'https.port=' + DEFAULTS['http.port'])
edit(app, "conf/application.conf", 232,
'play.ssl.netty.pipeline = play.server.FlashPolicyHandler,org.jboss.netty.handler.codec.http.HttpRequestDecoder,play.server.StreamChunkAggregator,org.jboss.netty.handler.codec.http.HttpResponseEncoder,org.jboss.netty.handler.codec.http.HttpContentCompressor,org.jboss.netty.handler.stream.ChunkedWriteHandler,play.server.ssl.SslPlayHandler')
create(app, 'conf/host.key')
Expand Down Expand Up @@ -145,7 +146,7 @@ def testSSLConfig(self):
step("Send request to https")

browser = mechanize.Browser()
response = browser.open('https://' + self.DEFAULTS['host'] + ':' + self.DEFAULTS['http.port'])
response = browser.open('https://' + DEFAULTS['host'] + ':' + DEFAULTS['http.port'])

step("check that ssl message is logged")
self.assertTrue(waitFor(self.play, 'I am ssl secured!'))
Expand All @@ -169,8 +170,7 @@ def testSSLConfig(self):

step("Send request to https")

browser = mechanize.Browser()
response = browser.open('https://' + self.DEFAULTS['host'] + ':' + self.DEFAULTS['http.port'])
browserOpen('https://' + DEFAULTS['host'] + ':' + DEFAULTS['http.port'])

step("check that ssl message is logged")
self.assertTrue(waitFor(self.play, 'I am ssl secured!'))
Expand All @@ -196,7 +196,7 @@ def testLogLevelsAndLog4jConfig(self):
self.play.wait()

app = '%s/loglevelsapp' % self.working_directory
edit(app, "conf/application.conf", 32, 'http.port=' + self.DEFAULTS['http.port'])
edit(app, "conf/application.conf", 32, 'http.port=' + DEFAULTS['http.port'])

# inserting some log-statements in our controller
insert(app, "app/controllers/Application.java", 13, ' Logger.debug("I am a debug message");')
Expand All @@ -210,14 +210,11 @@ def testLogLevelsAndLog4jConfig(self):

with callPlay(self, ['run', app]) as self.play:
# wait for play to be ready
self.assertTrue(waitFor(self.play, 'Listening for HTTP on port ' + self.DEFAULTS['http.port']))
self.assertTrue(waitFor(self.play, 'Listening for HTTP on port ' + DEFAULTS['http.port']))

step("Send request to trigger some logging")

browser = mechanize.Browser()
browser.set_handle_robots(False)

response = browser.open('http://' + self.DEFAULTS['host'] + ':' + self.DEFAULTS['http.port'])
response = browserOpen('http://' + DEFAULTS['host'] + ':' + DEFAULTS['http.port'])

step("check that only info log message is logged")
self.assertTrue(waitForWithFail(self.play, 'I am an info message', 'I am a debug message'))
Expand Down Expand Up @@ -249,12 +246,11 @@ def testLogLevelsAndLog4jConfig(self):

with callPlay(self, ['run', app]) as self.play:
# wait for play to be ready
self.assertTrue(waitFor(self.play, 'Listening for HTTP on port ' + self.DEFAULTS['http.port']))
self.assertTrue(waitFor(self.play, 'Listening for HTTP on port ' + DEFAULTS['http.port']))

step("Send request to trigger some logging")

browser = mechanize.Browser()
response = browser.open('http://' + self.DEFAULTS['host'] + ':' + self.DEFAULTS['http.port'])
response = browserOpen('http://' + DEFAULTS['host'] + ':' + DEFAULTS['http.port'])

step("check that both debug and info message is logged")
self.assertTrue(waitFor(self.play, 'I am a debug message'))
Expand All @@ -280,7 +276,7 @@ def testCreateAndRunForJobProject(self):
self.play.wait()
app = '%s/jobapp' % self.working_directory

edit(app, "conf/application.conf", 32, 'https.port=' + self.DEFAULTS['http.port'])
edit(app, "conf/application.conf", 32, 'https.port=' + DEFAULTS['http.port'])
# create our first job - which is executed sync on startup with @OnApplicationStart

createDir(app, 'app/jobs')
Expand All @@ -305,12 +301,11 @@ def testCreateAndRunForJobProject(self):

self.play = callPlay(self, ['run', app])
# wait for play to be ready
self.assertTrue(waitFor(self.play, 'Listening for HTTP on port ' + self.DEFAULTS['http.port']))
self.assertTrue(waitFor(self.play, 'Listening for HTTP on port ' + DEFAULTS['http.port']))

step("Send request to start app")

browser = mechanize.Browser()
response = browser.open('http://' + self.DEFAULTS['host'] + ':' + self.DEFAULTS['http.port'])
response = browserOpen('http://' + DEFAULTS['host'] + ':' + DEFAULTS['http.port'])

step("check that job completed before processing request")
self.assertTrue(waitFor(self.play, 'Job done'))
Expand All @@ -329,12 +324,10 @@ def testCreateAndRunForJobProject(self):

self.play = callPlay(self, ['run', app])
# wait for play to be ready
self.assertTrue(waitFor(self.play, 'Listening for HTTP on port ' + self.DEFAULTS['http.port']))
self.assertTrue(waitFor(self.play, 'Listening for HTTP on port ' + DEFAULTS['http.port']))

step("Send request to start app")

browser = mechanize.Browser()
response = browser.open('http://' + self.DEFAULTS['host'] + ':' + self.DEFAULTS['http.port'])
response = browserOpen('http://' + DEFAULTS['host'] + ':' + DEFAULTS['http.port'])

step("check that the request is processed before the job finishes")
self.assertTrue(waitFor(self.play, 'Processing request'))
Expand Down Expand Up @@ -379,7 +372,7 @@ def testSimpleProjectCreation(self):
step('Run the newly created application')

self.play = callPlay(self, ['run', app])
self.assertTrue(waitFor(self.play, 'Listening for HTTP on port ' + self.DEFAULTS['http.port']))
self.assertTrue(waitFor(self.play, 'Listening for HTTP on port ' + DEFAULTS['http.port']))

# Start a browser
step('Start a browser')
Expand All @@ -389,7 +382,7 @@ def testSimpleProjectCreation(self):
# Open the home page
step('Open the home page')

response = browser.open('http://' + self.DEFAULTS['host'] + ':' + self.DEFAULTS['http.port'])
response = browser.open('http://' + DEFAULTS['host'] + ':' + DEFAULTS['http.port'])
self.assertTrue(waitFor(self.play, "Application 'YOP' is now started !"))
self.assertTrue(browser.viewing_html())
self.assertTrue(browser.title() == 'Your application is ready !')
Expand All @@ -402,7 +395,7 @@ def testSimpleProjectCreation(self):

browser.addheaders = [("Accept-Language", "en")]
response = browser.open(
'http://' + self.DEFAULTS['host'] + ':' + self.DEFAULTS['http.port'] + '/@documentation')
'http://' + DEFAULTS['host'] + ':' + DEFAULTS['http.port'] + '/@documentation')
self.assertTrue(browser.viewing_html())
self.assertTrue(browser.title() == 'Play manual - Documentation')
html = response.get_data()
Expand Down Expand Up @@ -498,7 +491,7 @@ def testSimpleProjectCreation(self):
self.assertTrue(html.count(b'Hello !!'))

response = browser.open(
'http://' + self.DEFAULTS['host'] + ':' + self.DEFAULTS['http.port'] + '/?name=Guillaume')
'http://' + DEFAULTS['host'] + ':' + DEFAULTS['http.port'] + '/?name=Guillaume')
self.assertTrue(browser.viewing_html())
self.assertTrue(browser.title() == 'Hello world app')
html = response.get_data()
Expand Down Expand Up @@ -667,7 +660,7 @@ def testSimpleProjectCreation(self):

insert(app, 'conf/routes', 7, "GET /hello Hello.hello")
try:
response = browser.open('http://' + self.DEFAULTS['host'] + ':' + self.DEFAULTS['http.port'] + '/hello')
response = browser.open('http://' + DEFAULTS['host'] + ':' + DEFAULTS['http.port'] + '/hello')
self.fail()
except urllib.error.HTTPError as error:
self.assertTrue(browser.viewing_html())
Expand Down Expand Up @@ -855,53 +848,52 @@ def timeout(process):
timeoutOccurred = True


def killPlay(process, http='http'):
def killPlay(process, http='http', host=DEFAULTS['host'], port=DEFAULTS['http.port'], ):
print("kill %s" % process.pid)
try:
urllib.request.urlopen('%s://localhost:9000/@kill' % http)
url = "{}://{}:{}/@kill".format(http, host, port)
p = subprocess.Popen(["curl", url], stdout=subprocess.PIPE)

print(p.communicate())

# urllib.request.urlopen("{}://{}:{}/@kill".format(http, host, port))


# print("terminate" )
# process.terminate()
#
# print("process 1 (thread): kill process 2 (pid %s)" % process.pid)
# process.kill()
# print("process 1 (thread): close process 2 stdout pipe (fd %s)" % process.stdout.fileno())
#
# print("stdout" )
# if process.stdout:
# process.stdout.close()
#
# if process.stderr:
# process.stderr.close()
#
# if process.stdin:
# process.stdin.close()
#
#
# print("wait" )
# process.wait(10)
# print("wait" )
# process.wait()
#
# print("KILLED")
# # kill subprocess tree, because calling urllib.urlopen(f"{http}://localhost:9000/@kill") is not enough
# while True:
# if process.poll() is None:
# print("Kill Play subprocess")
# os.kill(process.pid, SIGTERM)
# process.wait(3)
# else:
# print("KILLED")
# return
except:
pass
# urllib.request.urlopen(f"{http}://" + 'localhost' + ':' + '9001' + '/@kill')

# print("terminate" )
# process.terminate()
#
# print("process 1 (thread): kill process 2 (pid %s)" % process.pid)
# process.kill()
# print("process 1 (thread): close process 2 stdout pipe (fd %s)" % process.stdout.fileno())
#
# print("stdout" )
# if process.stdout:
# process.stdout.close()
#
# if process.stderr:
# process.stderr.close()
#
# if process.stdin:
# process.stdin.close()
#
#
# print("wait" )
# process.wait(10)
# print("wait" )
# process.wait()
# #
# id = process.pid
#
# print("os.kill %s" % id )
#
# # os.kill(id, SIGTERM)
#
# print("KILLED")
# # kill subprocess tree, because calling urllib.urlopen(f"{http}://localhost:9000/@kill") is not enough
# while True:
# if process.poll() is None:
# print("Kill Play subprocess")
# os.kill(process.pid, SIGTERM)
# process.wait(3)
# else:
# print("KILLED")
# return


def step(msg):
Expand Down Expand Up @@ -962,6 +954,13 @@ def rename(app, fro, to):
os.rename(os.path.join(app, fro), os.path.join(app, to))


def browserOpen(url):
browser = mechanize.Browser()
browser.set_handle_robots(False)
response = browser.open(url)
return response


if __name__ == '__main__':
# thanks to: https://stackoverflow.com/a/35960702/3221476
try:
Expand Down

0 comments on commit 4454c8f

Please sign in to comment.