// Coordinate creates a new coordinate interface. This generally should be // only called once. If the backend has in-process state, such as a // database connection pool or an in-memory story, calling this multiple // times will create multiple copies of that state. In particular, if // b.Implementation is "memory", multiple calls to this will create // multiple independent coordinate "worlds". // // If b.Implementation does not match a known implementation, returns // an error. It is assumed that Set() will validate at least the // implementation. The choice of implementation can also produce // errors (invalid connection string, etc.) func (b *Backend) Coordinate() (coordinate.Coordinate, error) { switch b.Implementation { case "http", "https": return restclient.New(b.String()) case "memory": return memory.New(), nil case "postgres": return postgres.New(b.Address) default: return nil, errors.New("unknown coordinate backend " + b.Implementation) } }
func init() { // This sets up an object stack where the REST client code talks // to the REST server code, which points at an in-memory backend. memBackend := memory.NewWithClock(coordinatetest.Clock) router := restserver.NewRouter(memBackend) server := httptest.NewServer(router) backend, err := restclient.New(server.URL) if err != nil { panic(err) } coordinatetest.Coordinate = backend }
func TestEmptyURL(t *testing.T) { _, err := restclient.New("") if err == nil { t.Fatal("Expected error when given empty URL.") } }