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