func TestIdGeneratorNoneEmpty(t *testing.T) { // prepare data first. id := struct { ID uint64 `json:"id"` }{4} // save the id into local disk path := filepath.Join(orderDir, "test1_sky."+idExt) err := util.SaveJSON(path, id, 0600) assert.Nil(t, err) // remove file defer os.RemoveAll(filepath.Join(orderDir, "test1_sky."+idExt)) idg := newIDGenerator("test1/sky") closing := make(chan bool) go idg.Run(closing) for i := 1; i < 10; i++ { nid := idg.GetID() expected := uint64(4 + i) if nid != expected { t.Fatal("id error") } } close(closing) // check the file value. time.Sleep(500 * time.Millisecond) err = util.LoadJSON(path, &id) assert.Nil(t, err) if id.ID != 13 { t.Fatal("sync file failed") } }
// Saves known peers to disk as a newline delimited list of addresses to // <dir><PeerDatabaseFilename> func (self Peerlist) Save(dir string) error { logger.Debug("PEX: SavingPeerList") filename := PeerDatabaseFilename fn := filepath.Join(dir, filename) err := util.SaveJSON(fn, self, 0600) if err != nil { logger.Notice("SavePeerList Failed: %s", err) } return err }
func (ig IDGenerator) Run(closing chan bool) { id := struct { ID uint64 `json:"id"` }{} if _, err := os.Stat(ig.Path); !os.IsNotExist(err) { if err := util.LoadJSON(ig.Path, &id); err != nil { panic(err) } } for { select { case <-closing: return default: id.ID += 1 ig.IDC <- id.ID if err := util.SaveJSON(ig.Path, id, 0600); err != nil { panic(err) } } } }
// Run start the manager, tm is the match tick time, closing is used for stopping the manager from running. func (m *Manager) Start(tm time.Duration, closing chan bool) { // start the id generators for _, g := range m.idg { go g.Run(closing) } // start the match timer. wg := sync.WaitGroup{} for p, bk := range m.books { wg.Add(1) go func(cp string, b *Book, orderChan chan Order, c chan bool, w *sync.WaitGroup) { orders := []Order{} for { select { case <-c: w.Done() return case <-time.After(tm): orders = b.Match() for _, o := range orders { orderChan <- o } // update order book in local disk. pairs := strings.Split(cp, "/") if len(pairs) != 2 { panic("error coin pair name") } filename := strings.Join(pairs, "_") if err := util.SaveJSON(filepath.Join(orderDir, filename+"."+orderExt), b.Copy().ToMarshalable(), 0600); err != nil { panic(err) } } } }(p, bk, m.chans[p], closing, &wg) } wg.Wait() }
func TestLoadManager(t *testing.T) { // prepare data coinPair := []string{"test", "sky"} bk := Book{} var BidOrderList = []Order{ Order{ID: 1, Type: Bid, Price: 100, CreatedAt: 132424, Amount: 1, RestAmt: 1}, Order{ID: 2, Type: Bid, Price: 102, CreatedAt: 132425, Amount: 1, RestAmt: 1}, Order{ID: 3, Type: Bid, Price: 103, CreatedAt: 132428, Amount: 1, RestAmt: 1}, Order{ID: 4, Type: Bid, Price: 101, CreatedAt: 132429, Amount: 1, RestAmt: 1}, Order{ID: 5, Type: Bid, Price: 103, CreatedAt: 132430, Amount: 1, RestAmt: 1}, } var AskOrderList = []Order{ Order{ID: 6, Type: Ask, Price: 100, CreatedAt: 132424, Amount: 1, RestAmt: 1}, Order{ID: 7, Type: Ask, Price: 102, CreatedAt: 132425, Amount: 1, RestAmt: 1}, Order{ID: 8, Type: Ask, Price: 101, CreatedAt: 132429, Amount: 1, RestAmt: 1}, Order{ID: 9, Type: Ask, Price: 103, CreatedAt: 132428, Amount: 1, RestAmt: 1}, Order{ID: 10, Type: Ask, Price: 103, CreatedAt: 132438, Amount: 1, RestAmt: 1}, } for _, od := range BidOrderList { bk.AddBid(od) } for _, od := range AskOrderList { bk.AddAsk(od) } // write book to files. err := util.SaveJSON(filepath.Join(orderDir, strings.Join(coinPair, "_")+"."+orderExt), bk.ToMarshalable(), 0600) assert.Nil(t, err) m, err := LoadManager() assert.Nil(t, err) bk1 := m.GetBook(strings.Join(coinPair, "/")) assert.Equal(t, bk, bk1) }
// Saves to filename func (self *ReadableWallet) Save(filename string) error { logger.Info("Saving readable wallet to %s with filename %s", filename, self.Filename) return util.SaveJSON(filename, self, 0600) }
// persistance to disc. Save as JSON func (self *ExchangeAccountManager) save() error { logger.Debug("save accounts") a := self.ToMarshalable() // for self.Accounts return util.SaveJSON(filepath.Join(acntDir, acntName), a, 0600) }
// Saves to filename func (self *ReadableWallet) Save(filename string) error { return util.SaveJSON(filename, self, 0600) }