summaryrefslogtreecommitdiffstats
path: root/javatests/com/google/gerrit/index/query/LazyDataSourceTest.java
blob: 7064f648d43c24d9f4d4fe0327833530212afe01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright (C) 2019 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.gerrit.index.query;

import static com.google.gerrit.testing.GerritJUnit.assertThrows;

import com.google.common.collect.ImmutableList;
import com.google.gerrit.server.query.change.ChangeData;
import com.google.gerrit.server.query.change.ChangeDataSource;
import com.google.gerrit.server.query.change.OrSource;
import java.util.Collection;
import java.util.Iterator;
import org.junit.Test;

/**
 * Tests that boolean data sources are lazy in that they don't call {@link ResultSet#toList()} or
 * {@link ResultSet#toList()}. This is necessary because it allows Gerrit to send multiple queries
 * to the index in parallel, have the results come in asynchronously and wait for them only when we
 * call aforementioned methods on the {@link ResultSet}.
 */
public class LazyDataSourceTest {

  /** Helper to avoid a mock which would be hard to create because of the type inference. */
  static class LazyPredicate extends Predicate<ChangeData> implements ChangeDataSource {
    @Override
    public int getCardinality() {
      return 1;
    }

    @Override
    public ResultSet<ChangeData> read() {
      return new FailingResultSet<>();
    }

    @Override
    public ResultSet<FieldBundle> readRaw() {
      return new FailingResultSet<>();
    }

    @Override
    public Predicate<ChangeData> copy(Collection<? extends Predicate<ChangeData>> children) {
      throw new UnsupportedOperationException("not implemented");
    }

    @Override
    public int hashCode() {
      throw new UnsupportedOperationException("not implemented");
    }

    @Override
    public boolean equals(Object other) {
      throw new UnsupportedOperationException("not implemented");
    }

    @Override
    public boolean hasChange() {
      throw new UnsupportedOperationException("not implemented");
    }
  }

  /** Implementation that throws {@link AssertionError} when accessing results. */
  static class FailingResultSet<T> implements ResultSet<T> {
    @Override
    public Iterator<T> iterator() {
      throw new AssertionError(
          "called iterator() on the result set, but shouldn't have because the data source must be lazy");
    }

    @Override
    public ImmutableList<T> toList() {
      throw new AssertionError(
          "called toList() on the result set, but shouldn't have because the data source must be lazy");
    }

    @Override
    public void close() {
      // No-op
    }
  }

  @Test
  public void andSourceIsLazy() {
    AndSource<ChangeData> and = new AndSource<>(ImmutableList.of(new LazyPredicate()));
    ResultSet<ChangeData> resultSet = and.read();
    assertThrows(AssertionError.class, () -> resultSet.toList());
  }

  @Test
  public void orSourceIsLazy() {
    OrSource or = new OrSource(ImmutableList.of(new LazyPredicate()));
    ResultSet<ChangeData> resultSet = or.read();
    assertThrows(AssertionError.class, () -> resultSet.toList());
  }
}