Examples: Add Python 2&3 support

This commit is contained in:
Roland Dobai
2018-09-10 15:13:47 +02:00
parent 96cd3b75cd
commit 17b7959de9
10 changed files with 274 additions and 216 deletions

View File

@@ -14,6 +14,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
from builtins import str
from builtins import range
import imp
import re
import os
@@ -47,20 +52,20 @@ def test_examples_protocol_http_server_persistence(env, extra_data):
# Get binary file
binary_file = os.path.join(dut1.app.binary_path, "persistent_sockets.bin")
bin_size = os.path.getsize(binary_file)
IDF.log_performance("http_server_bin_size", "{}KB".format(bin_size/1024))
IDF.check_performance("http_server_bin_size", bin_size/1024)
IDF.log_performance("http_server_bin_size", "{}KB".format(bin_size//1024))
IDF.check_performance("http_server_bin_size", bin_size//1024)
# Upload binary and start testing
print "Starting http_server persistance test app"
print("Starting http_server persistance test app")
dut1.start_app()
# Parse IP address of STA
print "Waiting to connect with AP"
print("Waiting to connect with AP")
got_ip = dut1.expect(re.compile(r"(?:[\s\S]*)Got IP: '(\d+.\d+.\d+.\d+)'"), timeout=120)[0]
got_port = dut1.expect(re.compile(r"(?:[\s\S]*)Starting server on port: '(\d+)'"), timeout=30)[0]
print "Got IP : " + got_ip
print "Got Port : " + got_port
print("Got IP : " + got_ip)
print("Got Port : " + got_port)
# Expected Logs
dut1.expect("Registering URI handlers", timeout=30)
@@ -80,7 +85,7 @@ def test_examples_protocol_http_server_persistence(env, extra_data):
# Retest PUT request and change session context value
num = random.randint(0,100)
print "Adding :", num
print("Adding :", num)
client.putreq(conn, "/adder", str(num))
visitor += 1
adder += num
@@ -98,7 +103,7 @@ def test_examples_protocol_http_server_persistence(env, extra_data):
# Test POST request and session persistence
random_nums = [random.randint(0,100) for _ in range(100)]
for num in random_nums:
print "Adding :", num
print("Adding :", num)
client.postreq(conn, "/adder", str(num))
visitor += 1
adder += num
@@ -106,19 +111,19 @@ def test_examples_protocol_http_server_persistence(env, extra_data):
dut1.expect("/adder handler read " + str(num), timeout=30)
# Test GET request and session persistence
print "Matching final sum :", adder
if client.getreq(conn, "/adder") != str(adder):
print("Matching final sum :", adder)
if client.getreq(conn, "/adder").decode() != str(adder):
raise RuntimeError
visitor += 1
dut1.expect("/adder visitor count = " + str(visitor), timeout=30)
dut1.expect("/adder GET handler send " + str(adder), timeout=30)
print "Ending session"
print("Ending session")
# Close connection and check for invocation of context "Free" function
client.end_session(conn)
dut1.expect("/adder Free Context function called", timeout=30)
print "Validating user context data"
print("Validating user context data")
# Start another session to check user context data
conn2 = client.start_session(got_ip, got_port)
num = random.randint(0,100)

View File

@@ -14,11 +14,17 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import httplib
from __future__ import print_function
from __future__ import unicode_literals
from future import standard_library
standard_library.install_aliases()
from builtins import str
from builtins import range
import http.client
import argparse
def start_session (ip, port):
return httplib.HTTPConnection(ip, int(port), timeout=15)
return http.client.HTTPConnection(ip, int(port), timeout=15)
def end_session (conn):
conn.close()
@@ -28,11 +34,11 @@ def getreq (conn, path, verbose = False):
resp = conn.getresponse()
data = resp.read()
if verbose:
print "GET : ", path
print "Status : ", resp.status
print "Reason : ", resp.reason
print "Data length : ", len(data)
print "Data content : ", data
print("GET : ", path)
print("Status : ", resp.status)
print("Reason : ", resp.reason)
print("Data length : ", len(data))
print("Data content : ", data)
return data
def postreq (conn, path, data, verbose = False):
@@ -40,11 +46,11 @@ def postreq (conn, path, data, verbose = False):
resp = conn.getresponse()
data = resp.read()
if verbose:
print "POST : ", data
print "Status : ", resp.status
print "Reason : ", resp.reason
print "Data length : ", len(data)
print "Data content : ", data
print("POST : ", data)
print("Status : ", resp.status)
print("Reason : ", resp.reason)
print("Data length : ", len(data))
print("Data content : ", data)
return data
def putreq (conn, path, body, verbose = False):
@@ -52,11 +58,11 @@ def putreq (conn, path, body, verbose = False):
resp = conn.getresponse()
data = resp.read()
if verbose:
print "PUT : ", path, body
print "Status : ", resp.status
print "Reason : ", resp.reason
print "Data length : ", len(data)
print "Data content : ", data
print("PUT : ", path, body)
print("Status : ", resp.status)
print("Reason : ", resp.reason)
print("Data length : ", len(data))
print("Data content : ", data)
return data
if __name__ == '__main__':
@@ -73,22 +79,22 @@ if __name__ == '__main__':
N = args['N']
# Establish HTTP connection
print "Connecting to => " + ip + ":" + port
print("Connecting to => " + ip + ":" + port)
conn = start_session (ip, port)
# Reset adder context to specified value(0)
# -- Not needed as new connection will always
# -- have zero value of the accumulator
print "Reset the accumulator to 0"
print("Reset the accumulator to 0")
putreq (conn, "/adder", str(0))
# Sum numbers from 1 to specified value(N)
print "Summing numbers from 1 to " + str(N)
for i in xrange(1, N+1):
print("Summing numbers from 1 to " + str(N))
for i in range(1, N+1):
postreq (conn, "/adder", str(i))
# Fetch the result
print "Result :", getreq (conn, "/adder")
print("Result :", getreq (conn, "/adder"))
# Close HTTP connection
end_session (conn)