Skip to content

Commit

Permalink
[playframework#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 ffa9ef4
Showing 1 changed file with 58 additions and 66 deletions.
124 changes: 58 additions & 66 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,12 @@


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

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

}

@unittest.skip
def testSSLConfig(self):
Expand All @@ -48,7 +47,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 +144,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 @@ -170,7 +169,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 @@ -196,7 +195,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 +209,14 @@ 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 = browser.open('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 +248,12 @@ 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 = browser.open('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 +279,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 +304,12 @@ 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 = browser.open('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 +328,12 @@ 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 = browser.open('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 +378,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 +388,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 +401,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 +497,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 +666,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 +854,46 @@ 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)
urllib.request.urlopen('%s://s:%s/@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

0 comments on commit ffa9ef4

Please sign in to comment.