nit: Added link to `CONTRIBUTING.md` from the README
[nit.git] / lib / popcorn / tests / base_tests.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import popcorn
16 import pthreads
17
18 redef class Sys
19 var test_host = "localhost"
20
21 # Return a new port for each instance
22 fun test_port: Int do
23 srand
24 return 10000+20000.rand
25 end
26 end
27
28 class AppThread
29 super Thread
30
31 var host: String
32 var port: Int
33 var app: App
34
35 redef fun main
36 do
37 # Hide testing concept to force nitcorn to actually run
38 "NIT_TESTING".setenv("false")
39 app.quiet = true
40 app.listen(host, port)
41 return null
42 end
43 end
44
45 class ClientThread
46 super Thread
47
48 var host: String
49 var port: Int
50
51 redef fun main do return null
52
53 # Regex to catch and hide the port from the output to get consistent results
54 var host_re: Regex = "localhost:\[0-9\]+".to_re
55
56 fun system(cmd: String, title: nullable String)
57 do
58 title = title or else cmd
59 title = title.replace(host_re, "localhost:*****")
60 print "\n[Client] {title}"
61 sys.system cmd
62 end
63 end