blob: 3c1abb76389f8dd80a2fcdf6ce0727805a408a28 [file] [log] [blame]
Alexei Frolovd0b2d482019-12-04 11:06:23 -08001.. _chapter-test-server:
2
3.. default-domain:: go
4
5.. highlight:: go
6
7--
8Go
9--
10
11Server
12------
13
14.. TODO(frolv): Build and host documentation using godoc and link to it.
15
16Full API documentation for the server library can be found here.
17
18Example program
19^^^^^^^^^^^^^^^
20
21The code below implements a very basic test server with two test workers which
22print out the path of the tests they are scheduled to run.
23
24.. code::
25
26 package main
27
28 import (
29 "flag"
30 "log"
31
Armando Montanezc0ad9782019-12-30 14:58:00 -080032 pb "pigweed.dev/proto/pw_target_runner/target_runner_pb"
33 "pigweed.dev/pw_target_runner"
Alexei Frolovd0b2d482019-12-04 11:06:23 -080034 )
35
36 // Custom test worker that implements the interface server.UnitTestRunner.
37 type MyWorker struct {
38 id int
39 }
40
41 func (w *MyWorker) WorkerStart() error {
42 log.Printf("Starting test worker %d\n", w.id)
43 return nil
44 }
45
46 func (w *MyWorker) WorkerExit() {
47 log.Printf("Exiting test worker %d\n", w.id)
48 }
49
50 func (w *MyWorker) HandleRunRequest(req *server.UnitTestRunRequest) *server.UnitTestRunResponse {
51 log.Printf("Worker %d running unit test %s\n", w.id, req.Path)
52 return &server.UnitTestRunResponse{
53 Output: []byte("Success!"),
54 Status: pb.TestStatus_SUCCESS,
55 }
56 }
57
58 // To run:
59 //
60 // $ go build -o server
61 // $ ./server -port 80
62 //
63 func main() {
64 port := flag.Int("port", 8080, "Port on which to run server")
65 flag.Parse()
66
67 s := server.New()
68
69 // Create and register as many unit test workers as you need.
70 s.RegisterWorker(&MyWorker{id: 0})
71 s.RegisterWorker(&MyWorker{id: 1})
72
73 if err := s.Bind(*port); err != nil {
74 log.Fatalf("Failed to bind to port %d: %v", *port, err)
75 }
76
77 if err := s.Serve(); err != nil {
78 log.Fatalf("Failed to start server: %v", err)
79 }
80 }