go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/server/dsmapper/dsmapperlite/dsmapperlite_test.go (about)

     1  // Copyright 2018 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package dsmapperlite
    16  
    17  import (
    18  	"context"
    19  	"errors"
    20  	"sync"
    21  	"sync/atomic"
    22  	"testing"
    23  
    24  	"go.chromium.org/luci/gae/impl/memory"
    25  	"go.chromium.org/luci/gae/service/datastore"
    26  )
    27  
    28  type intEnt struct {
    29  	ID int64 `gae:"$id"`
    30  }
    31  
    32  func TestMap(t *testing.T) {
    33  	t.Parallel()
    34  
    35  	ctx := memory.Use(context.Background())
    36  
    37  	const count = 1000
    38  	for i := 0; i < count; i++ {
    39  		if err := datastore.Put(ctx, &intEnt{ID: int64(i + 1)}); err != nil {
    40  			t.Fatalf("Storing entity: %s", err)
    41  		}
    42  	}
    43  	datastore.GetTestable(ctx).CatchupIndexes()
    44  
    45  	t.Run("visits all", func(t *testing.T) {
    46  		m := sync.Mutex{}
    47  		seen := map[int64]int{}
    48  
    49  		err := Map(ctx, datastore.NewQuery("intEnt"), 10, 256, func(ctx context.Context, _ int, e *intEnt) error {
    50  			m.Lock()
    51  			seen[e.ID] += 1
    52  			m.Unlock()
    53  			return nil
    54  		})
    55  		if err != nil {
    56  			t.Fatalf("Unexpected error: %s", err)
    57  		}
    58  
    59  		if len(seen) != count {
    60  			t.Fatalf("Expected to visit %d entities, but visited %d", count, len(seen))
    61  		}
    62  		for key, visits := range seen {
    63  			if visits != 1 {
    64  				t.Fatalf("Entity %d was visited more than once: %d", key, visits)
    65  			}
    66  		}
    67  	})
    68  
    69  	t.Run("aborts on errors", func(t *testing.T) {
    70  		var visits atomic.Int64
    71  		err := Map(ctx, datastore.NewQuery("intEnt"), 10, 256, func(ctx context.Context, _ int, e *intEnt) error {
    72  			if visits.Add(1) == 100 {
    73  				return errors.New("boom")
    74  			}
    75  			return nil
    76  		})
    77  		if err == nil {
    78  			t.Fatalf("Unexpectedly succeeded")
    79  		}
    80  	})
    81  }