Example #1
0
func TestDump(t *testing.T) {
	aelog.DefaultLogger, _ = aelog.New("", 0)
	for i := 0; i < 5; i++ {
		TestCount -= i * 10
		makeTestData()
		file := "test.dump"
		defer os.Remove(file)
		defer os.Remove(file + ".td")
		n, err := DumpTo(file, TestData)
		if err != nil {
			t.Errorf("Dump has error: %v", err)
		}
		if n <= 0 {
			t.Errorf("Dump write %d bytes. Wrong.", n)
		}

		data := new(TD)
		err, exists := LoadData(file, data)
		if err != nil {
			t.Errorf("LoadData has error: %v", err)
		}
		if !exists {
			t.Errorf("File must be exists")
		}

		if !data.Assert() {
			t.Errorf("Data mismatched")
		}
	}
}
Example #2
0
func main() {
	defer func() {
		/* if r := recover(); r != nil {
		   	fmt.Println("Error!")
		   	fmt.Println(r)
		   }
		*/
	}()

	if *cpuprofile != "" {
		f, err := os.Create(*cpuprofile)
		if err != nil {
			panic(err)
		}
		pprof.StartCPUProfile(f)
		defer pprof.StopCPUProfile()
	}

	if *httpprofile {
		go func() {
			log.Println(ghttp.ListenAndServe(":6060", nil))
		}()
	}

	var err error

	if *isPrintVersion {
		printVersion()
		return
	}

	if *isPrintHelp {
		printHelp()
		return
	}

	// If configFile not specified - show help
	if *configFile == "" {
		printHelp()
		return
	}

	// Init config
	c := &config.Config{}
	c.ReadFile(*configFile)

	// Init logger
	aelog.DefaultLogger, err = aelog.New(c.LogFile, c.LogLevel)
	if err != nil {
		panic(err)
	}

	// Init storage
	stor := &storage.Storage{}
	stor.Init(c)
	err = stor.Open()
	if err != nil {
		panic(err)
	}
	defer stor.Close()

	// init access log is needed
	var al *aelog.AntLog
	if c.LogAccess != "" {
		al, err = aelog.New(c.LogAccess, aelog.LOG_PRINT)
		if err != nil {
			panic(err)
		}
	}

	// Run server
	http.RunServer(stor, al)

	// Run rpc server
	rpcserver.StartRpcServer(stor)

	aelog.Infof("Run working (use %d cpus)", c.CpuNum)

	interrupt := make(chan os.Signal, 1)
	signal.Notify(interrupt, syscall.SIGKILL, os.Interrupt, syscall.SIGTERM)
	sig := <-interrupt
	aelog.Infof("Catched signal %v. Stop server", sig)
	stor.Dump()
	aelog.Infoln("")
}