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