예제 #1
0
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")
	}
}
예제 #2
0
파일: pex.go 프로젝트: skycoin/skycoin
// Loads a newline delimited list of addresses from
// "<dir>/<PeerDatabaseFilename>"
func LoadPeerlist(dir string) (Peerlist, error) {
	peerlist := Peerlist{}
	fn := filepath.Join(dir, PeerDatabaseFilename)
	err := util.LoadJSON(fn, &peerlist)
	// if err != nil {
	// 	logger.Notice("LoadPeerList Failed: %s", err)
	// }
	return peerlist, err

}
예제 #3
0
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)
			}
		}
	}
}
예제 #4
0
func LoadReadableWalletEntry(filename string) (ReadableWalletEntry, error) {
	w := ReadableWalletEntry{}
	err := util.LoadJSON(filename, &w)
	return w, err
}
예제 #5
0
// Loads from filename
func (self *ReadableWallet) Load(filename string) error {
	return util.LoadJSON(filename, self)
}