Example #1
0
func makeState(ctx context.Context) (s state.State, err error) {
	cfg := getConfig()
	bucket := getBucket(ctx)

	// Open the specified file.
	f, err := os.Open(cfg.StateFile)
	switch {
	// Special case: if the error is that the file doesn't exist, ignore it.
	case os.IsNotExist(err):
		err = nil
		log.Println("No state file found. Using fresh state.")

	case err != nil:
		return
	}

	// If we opened a file above, load from it.
	if f != nil {
		defer f.Close()
		s, err = state.LoadState(f)
		if err != nil {
			err = fmt.Errorf("LoadState: %v", err)
			return
		}
	}

	// Throw out the existing score cache if requested.
	if *g_discardScoreCache {
		s.ScoresForFiles = state.NewScoreMap()
	}

	// Make sure there are no nil interface values.
	if s.ScoresForFiles == nil {
		s.ScoresForFiles = state.NewScoreMap()
	}

	// If we don't know the set of hex scores in the store, or the set of scores
	// is stale, re-list.
	age := time.Now().Sub(s.RelistTime)
	const maxAge = 30 * 24 * time.Hour

	if s.ExistingScores == nil || age > maxAge {
		log.Println("Listing existing scores...")

		s.RelistTime = time.Now()
		s.ExistingScores, err = buildExistingScores(ctx, bucket)
		if err != nil {
			err = fmt.Errorf("buildExistingScores: %v", err)
			return
		}
	}

	return
}
Example #2
0
func (t *ScoreMapTest) DecodingOverwritesContents() {
	key0 := state.ScoreMapKey{Path: "taco"}
	key1 := state.ScoreMapKey{Path: "burrito"}
	key2 := state.ScoreMapKey{Path: "enchilada"}

	// Source contents
	scores0 := []blob.Score{blob.ComputeScore([]byte("foo"))}
	scores1 := []blob.Score{blob.ComputeScore([]byte("bar"))}

	t.m.Set(key0, scores0)
	t.m.Set(key1, scores1)

	// Encode
	buf := new(bytes.Buffer)
	encoder := gob.NewEncoder(buf)
	AssertEq(nil, encoder.Encode(&t.m))

	// Destination
	decoded := state.NewScoreMap()
	decoded.Set(key0, scores1)
	decoded.Set(key2, scores0)

	// Decode
	decoder := gob.NewDecoder(buf)
	AssertEq(nil, decoder.Decode(&decoded))

	ExpectThat(decoded.Get(key0), DeepEquals(scores0))
	ExpectThat(decoded.Get(key1), DeepEquals(scores1))
	ExpectEq(nil, decoded.Get(key2))
}
Example #3
0
func (t *VisitorTest) SetUp(ti *TestInfo) {
	var err error

	t.ctx = ti.Ctx
	t.chunkSize = 8
	t.scoreMap = state.NewScoreMap()
	t.blobStore = mock_blob.NewMockStore(ti.MockController, "blobStore")
	t.clock.SetTime(time.Now())

	// Set up the directory.
	t.dir, err = ioutil.TempDir("", "visitor_test")
	AssertEq(nil, err)
}
Example #4
0
func (t *ScoreMapTest) SetUp(i *TestInfo) {
	t.m = state.NewScoreMap()
}