Go#
Pigweed AI summary: This section provides documentation for a Go server library, including an example program that implements a basic test server with two test workers. The code is provided and instructions are given for running the program.
Server#
Pigweed AI summary: This document provides API documentation for the server library and includes an example program that implements a basic test server with two test workers. The code for the example program is written in Go and includes custom test workers that implement the interface server.UnitTestRunner. The program can be run by building it and specifying the port on which to run the server.
Full API documentation for the server library can be found here.
Example program#
Pigweed AI summary: This code is an example program that implements a basic test server with two test workers that print out the path of the tests they are scheduled to run. The program is written in Go and uses the pigweed.dev/proto/pw_target_runner/target_runner_pb and pigweed.dev/pw_target_runner packages. The MyWorker struct is a custom test worker that implements the interface server.UnitTestRunner. The program can be run by building it and then running it with the specified port.
The code below implements a very basic test server with two test workers which print out the path of the tests they are scheduled to run.
package main
import (
"flag"
"log"
pb "pigweed.dev/proto/pw_target_runner/target_runner_pb"
"pigweed.dev/pw_target_runner"
)
// Custom test worker that implements the interface server.UnitTestRunner.
type MyWorker struct {
id int
}
func (w *MyWorker) WorkerStart() error {
log.Printf("Starting test worker %d\n", w.id)
return nil
}
func (w *MyWorker) WorkerExit() {
log.Printf("Exiting test worker %d\n", w.id)
}
func (w *MyWorker) HandleRunRequest(req *server.UnitTestRunRequest) *server.UnitTestRunResponse {
log.Printf("Worker %d running unit test %s\n", w.id, req.Path)
return &server.UnitTestRunResponse{
Output: []byte("Success!"),
Status: pb.TestStatus_SUCCESS,
}
}
// To run:
//
// $ go build -o server
// $ ./server -port 80
//
func main() {
port := flag.Int("port", 8080, "Port on which to run server")
flag.Parse()
s := server.New()
// Create and register as many unit test workers as you need.
s.RegisterWorker(&MyWorker{id: 0})
s.RegisterWorker(&MyWorker{id: 1})
if err := s.Bind(*port); err != nil {
log.Fatalf("Failed to bind to port %d: %v", *port, err)
}
if err := s.Serve(); err != nil {
log.Fatalf("Failed to start server: %v", err)
}
}