LCOV - code coverage report
Current view: top level - acceptance/testsuite/change - IndexOperations.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 66 66 100.0 %
Date: 2022-11-19 15:00:39 Functions: 17 17 100.0 %

          Line data    Source code
       1             : // Copyright (C) 2022 The Android Open Source Project
       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 com.google.gerrit.acceptance.testsuite.change;
      16             : 
      17             : import com.google.gerrit.acceptance.DisabledAccountIndex;
      18             : import com.google.gerrit.acceptance.DisabledChangeIndex;
      19             : import com.google.gerrit.acceptance.DisabledProjectIndex;
      20             : import com.google.gerrit.index.project.ProjectIndex;
      21             : import com.google.gerrit.index.project.ProjectIndexCollection;
      22             : import com.google.gerrit.server.index.account.AccountIndex;
      23             : import com.google.gerrit.server.index.account.AccountIndexCollection;
      24             : import com.google.gerrit.server.index.change.ChangeIndex;
      25             : import com.google.gerrit.server.index.change.ChangeIndexCollection;
      26             : import com.google.inject.Inject;
      27             : 
      28             : /** Helpers to enable and disable reads/writes to secondary indices during testing. */
      29             : public interface IndexOperations {
      30             :   /**
      31             :    * Disables reads from the secondary index that this instance is scoped to. Reads fail with {@code
      32             :    * UnsupportedOperationException}.
      33             :    */
      34             :   AutoCloseable disableReads();
      35             : 
      36             :   /**
      37             :    * Disables writes to the secondary index that this instance is scoped to. Writes fail with {@code
      38             :    * UnsupportedOperationException}.
      39             :    */
      40             :   AutoCloseable disableWrites();
      41             : 
      42             :   /** Disables reads from and writes to the secondary index that this instance is scoped to. */
      43             :   default AutoCloseable disableReadsAndWrites() {
      44           6 :     AutoCloseable reads = disableReads();
      45           6 :     AutoCloseable writes = disableWrites();
      46           6 :     return () -> {
      47           6 :       reads.close();
      48           6 :       writes.close();
      49           6 :     };
      50             :   }
      51             : 
      52           8 :   class Change implements IndexOperations {
      53             :     @Inject private ChangeIndexCollection indices;
      54             : 
      55             :     @Override
      56             :     public AutoCloseable disableReads() {
      57           5 :       ChangeIndex maybeDisabledSearchIndex = indices.getSearchIndex();
      58           5 :       if (!(maybeDisabledSearchIndex instanceof DisabledChangeIndex)) {
      59           5 :         indices.setSearchIndex(
      60             :             new DisabledChangeIndex(maybeDisabledSearchIndex), /* closeOld */ false);
      61             :       }
      62             : 
      63           5 :       return () -> {
      64           5 :         ChangeIndex maybeEnabledSearchIndex = indices.getSearchIndex();
      65           5 :         if (maybeEnabledSearchIndex instanceof DisabledChangeIndex) {
      66           5 :           indices.setSearchIndex(
      67           5 :               ((DisabledChangeIndex) maybeEnabledSearchIndex).unwrap(), /* closeOld */ false);
      68             :         }
      69           5 :       };
      70             :     }
      71             : 
      72             :     @Override
      73             :     public AutoCloseable disableWrites() {
      74           8 :       for (ChangeIndex i : indices.getWriteIndexes()) {
      75           8 :         if (!(i instanceof DisabledChangeIndex)) {
      76           8 :           indices.addWriteIndex(new DisabledChangeIndex(i));
      77             :         }
      78           8 :       }
      79           8 :       return () -> {
      80           8 :         for (ChangeIndex i : indices.getWriteIndexes()) {
      81           8 :           if (i instanceof DisabledChangeIndex) {
      82           8 :             indices.addWriteIndex(((DisabledChangeIndex) i).unwrap());
      83             :           }
      84           8 :         }
      85           8 :       };
      86             :     }
      87             :   }
      88             : 
      89           2 :   class Account implements IndexOperations {
      90             :     @Inject private AccountIndexCollection indices;
      91             : 
      92             :     @Override
      93             :     public AutoCloseable disableReads() {
      94           2 :       AccountIndex maybeDisabledSearchIndex = indices.getSearchIndex();
      95           2 :       if (!(maybeDisabledSearchIndex instanceof DisabledAccountIndex)) {
      96           2 :         indices.setSearchIndex(
      97             :             new DisabledAccountIndex(maybeDisabledSearchIndex), /* closeOld */ false);
      98             :       }
      99             : 
     100           2 :       return () -> {
     101           2 :         AccountIndex maybeEnabledSearchIndex = indices.getSearchIndex();
     102           2 :         if (maybeEnabledSearchIndex instanceof DisabledAccountIndex) {
     103           2 :           indices.setSearchIndex(
     104           2 :               ((DisabledAccountIndex) maybeEnabledSearchIndex).unwrap(), /* closeOld */ false);
     105             :         }
     106           2 :       };
     107             :     }
     108             : 
     109             :     @Override
     110             :     public AutoCloseable disableWrites() {
     111           2 :       for (AccountIndex i : indices.getWriteIndexes()) {
     112           2 :         if (!(i instanceof DisabledAccountIndex)) {
     113           2 :           indices.addWriteIndex(new DisabledAccountIndex(i));
     114             :         }
     115           2 :       }
     116           2 :       return () -> {
     117           2 :         for (AccountIndex i : indices.getWriteIndexes()) {
     118           2 :           if (i instanceof DisabledAccountIndex) {
     119           2 :             indices.addWriteIndex(((DisabledAccountIndex) i).unwrap());
     120             :           }
     121           2 :         }
     122           2 :       };
     123             :     }
     124             :   }
     125             : 
     126           1 :   class Project implements IndexOperations {
     127             :     @Inject private ProjectIndexCollection indices;
     128             : 
     129             :     @Override
     130             :     public AutoCloseable disableReads() {
     131           1 :       ProjectIndex maybeDisabledSearchIndex = indices.getSearchIndex();
     132           1 :       if (!(maybeDisabledSearchIndex instanceof DisabledProjectIndex)) {
     133           1 :         indices.setSearchIndex(
     134             :             new DisabledProjectIndex(maybeDisabledSearchIndex), /* closeOld */ false);
     135             :       }
     136             : 
     137           1 :       return () -> {
     138           1 :         ProjectIndex maybeEnabledSearchIndex = indices.getSearchIndex();
     139           1 :         if (maybeEnabledSearchIndex instanceof DisabledProjectIndex) {
     140           1 :           indices.setSearchIndex(
     141           1 :               ((DisabledProjectIndex) maybeEnabledSearchIndex).unwrap(), /* closeOld */ false);
     142             :         }
     143           1 :       };
     144             :     }
     145             : 
     146             :     @Override
     147             :     public AutoCloseable disableWrites() {
     148           1 :       for (ProjectIndex i : indices.getWriteIndexes()) {
     149           1 :         if (!(i instanceof DisabledProjectIndex)) {
     150           1 :           indices.addWriteIndex(new DisabledProjectIndex(i));
     151             :         }
     152           1 :       }
     153           1 :       return () -> {
     154           1 :         for (ProjectIndex i : indices.getWriteIndexes()) {
     155           1 :           if (i instanceof DisabledProjectIndex) {
     156           1 :             indices.addWriteIndex(((DisabledProjectIndex) i).unwrap());
     157             :           }
     158           1 :         }
     159           1 :       };
     160             :     }
     161             :   }
     162             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750