예제 #1
0
파일: app.go 프로젝트: ursiform/forest
func New(configFile string) *App {
	app := new(App)
	app.Config = new(Config)
	if len(configFile) > 0 {
		app.Config.File = configFile
	} else {
		app.Config.File = ConfigFile
	}
	app.Mux = bear.New()
	err := loadConfig(app)
	app.Log, _ = logger.New(app.Config.LogLevel)
	if err != nil {
		logger.MustWarn("%s was not loaded", app.Config.File)
	}
	if app.Config.Address == "" {
		app.Config.Address = address
		app.Log.Warn("address is not defined in %s, using default %s",
			app.Config.File, address)
	}
	app.durations = make(map[string]time.Duration)
	app.errors = make(map[string]string)
	app.messages = make(map[string]string)
	app.wares = make(map[string]func(ctx *bear.Context))
	initDefaults(app)
	return app
}
예제 #2
0
파일: sleuth.go 프로젝트: jmheidly/sleuth
// New is the entry point to the sleuth package. It returns a reference to a
// Client object that has joined the local network. If the config argument is
// nil, sleuth will use sensible defaults. If the Handler attribute of the
// config object is not set, sleuth will operate in client-only mode.
func New(config *Config) (*Client, error) {
	// Sanitize the configuration object.
	config = initConfig(config)
	// Use the same log level as the instantiator of the client. Because log level
	// is guaranteed to be correct in initConfig, errors can be ignored.
	log, _ := logger.New(config.logLevel)
	conn := &connection{group: config.group}
	if conn.server = config.Handler != nil; conn.server {
		conn.handler = config.Handler
		conn.name = config.Service
		if len(conn.name) == 0 {
			return nil, newError(errService, "config.Service not defined")
		}
	} else {
		log.Init("sleuth: config.Handler is nil, client-only mode")
	}
	if conn.adapter = config.Interface; len(conn.adapter) == 0 {
		log.Warn("sleuth: config.Interface not defined [%d]", warnInterface)
	}
	if conn.port = config.Port; conn.port == 0 {
		conn.port = port
	}
	if conn.version = config.Version; len(conn.version) == 0 {
		conn.version = "unknown"
	}
	node, err := newNode(conn, log)
	if err != nil {
		return nil, err.(*Error).escalate(errNew)
	}
	client := newClient(config.group, node, log)
	client.handler = conn.handler
	go listen(client)
	return client, nil
}
예제 #3
0
func TestClientDispatchEmpty(t *testing.T) {
	log, _ := logger.New(logger.Silent)
	c := newClient(GROUP, nil, log)
	err := c.dispatch([]byte{})
	if err == nil {
		t.Errorf("expected client dispatch to fail on empty payload")
		return
	}
	testCodes(t, err, []int{errDispatchHeader})
}
예제 #4
0
func TestClientDispatchBadAction(t *testing.T) {
	log, _ := logger.New(logger.Silent)
	c := newClient(GROUP, nil, log)
	err := c.dispatch([]byte(GROUP + "FAIL"))
	if err == nil {
		t.Errorf("expected client dispatch to fail on bad action")
		return
	}
	testCodes(t, err, []int{errDispatchAction})
}
예제 #5
0
func TestClientAddBadMember(t *testing.T) {
	log, _ := logger.New(logger.Silent)
	c := newClient(GROUP, nil, log)
	err := c.add(GROUP, "foo", "bar", "", "")
	if err == nil {
		t.Errorf("expected client dispatch to fail on bad member")
		return
	}
	testCodes(t, err, []int{errAdd})
}
예제 #6
0
func TestClientReplyBadPayload(t *testing.T) {
	log, _ := logger.New(logger.Silent)
	c := newClient(GROUP, nil, log)
	err := c.reply([]byte(""))
	if err == nil {
		t.Errorf("expected client reply to fail on bad payload")
		return
	}
	testCodes(t, err, []int{errUnzip, errReqUnmarshal, errREPL})
}
예제 #7
0
func TestClientReceiveBadHandle(t *testing.T) {
	log, _ := logger.New(logger.Silent)
	c := newClient(GROUP, nil, log)
	res := &response{Handle: "1"}
	err := c.receive(resMarshal(GROUP, res)[len(GROUP)+len(recv):])
	if err == nil {
		t.Errorf("expected client receive to fail on bad handle")
		return
	}
	testCodes(t, err, []int{errRECV})
}
예제 #8
0
func TestClientDoUnknownService(t *testing.T) {
	log, _ := logger.New(logger.Silent)
	c := newClient(GROUP, nil, log)
	req, _ := http.NewRequest("POST", "sleuth://foo/bar", nil)
	_, err := c.Do(req)
	if err == nil {
		t.Errorf("expected client Do to fail on unknown service")
		return
	}
	testCodes(t, err, []int{errUnknownService})
}
예제 #9
0
func TestClientRemove(t *testing.T) {
	log, _ := logger.New(logger.Silent)
	c := newClient(GROUP, nil, log)
	name := "foo"
	service := "baz"
	if c.services[service] != nil {
		t.Errorf("expected workers to be empty")
		return
	}
	c.add(GROUP, name, "node id", service, "v0.0.1")
	if c.services[service] == nil || !c.services[service].available() {
		t.Errorf("expected client add to succeed")
		return
	}
	c.remove(name)
	if c.services[service] != nil {
		t.Errorf("expected client remove to succeed")
		return
	}
}
예제 #10
0
func TestClientRemoveNonexistent(t *testing.T) {
	log, _ := logger.New(logger.Silent)
	c := newClient(GROUP, nil, log)
	c.remove("foo") // c.remove is a no op.
}