func TestMethodDispatch(t *testing.T) { a = 0 b = 0 called = 0 hooks := map[string]interface{}{ "eventA": []interface{}{"modA"}, "eventB": []interface{}{[]interface{}{"modB", "MethodB"}}, } ev := event.New(nil, hooks, newModule) if a != 0 { t.Fatal(a) } ev.Fire("eventA", "testA") if a != 1 { t.Fatal(a) } ev.Fire("eventA", "asdadsad") if a != 1 { t.Fatal(a) } ev.Fire("eventC") // Nothing should happend when we call a not existing event. if a != 1 { t.Fatal(a) } if b != 0 { t.Fatal(b) } ev.Fire("eventB", "testB") if b != 1 { t.Fatal(b) } }
func TestStatePreserving(t *testing.T) { a = 0 b = 0 called = 0 hooks := map[string]interface{}{ "eventA": []interface{}{"modA"}, } ev := event.New(nil, hooks, newModule) if called != 0 { t.Fatal(called) } for i := 0; i < 10; i++ { ev.Fire("eventA", "dummy data") if called != i+1 { t.Fatal(called) } } }
func New(session *mgo.Session, db *mgo.Database, w http.ResponseWriter, req *http.Request, config *config.Config) (t *Top, err error) { put := func(a ...interface{}) { io.WriteString(w, fmt.Sprint(a...)+"\n") } uni := &context.Uni{ Db: db, W: w, Req: req, Put: put, Dat: make(map[string]interface{}), Root: config.AbsPath, Path: req.URL.Path, NewModule: mod.NewModule, } err = uni.Req.ParseMultipartForm(1000000) // Should we handle the error return of this? if err != nil { return nil, err } mods := modifiers(uni.Req.Form) uni.Modifiers = mods // Not sure if not giving the db session to nonadmin installations increases security, but hey, one can never be too cautious, they dont need it anyway. if config.DBAdmMode { uni.Session = session } opt, opt_str, err := queryConfig(uni.Db, req.Host, config.CacheOpt) // Tricky part about the host, see comments at main_model. if err != nil { return nil, err } uni.Req.Host = scut.Host(req.Host, opt) uni.Opt = opt hooks, _ := uni.Opt["Hooks"].(map[string]interface{}) ev := event.New(uni, hooks, mod.NewModule) uni.Ev = ev uni.NewModule = ev.NewModuleProducer() uni.SetOriginalOpt(opt_str) uni.SetSecret(config.Secret) return &Top{uni, config}, nil }