|
| 1 | +/* |
| 2 | + * CommandsTest.java |
| 3 | + * |
| 4 | + * This source file is part of the FoundationDB open source project |
| 5 | + * |
| 6 | + * Copyright 2015-2025 Apple Inc. and the FoundationDB project authors |
| 7 | + * |
| 8 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | + * you may not use this file except in compliance with the License. |
| 10 | + * You may obtain a copy of the License at |
| 11 | + * |
| 12 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | + * |
| 14 | + * Unless required by applicable law or agreed to in writing, software |
| 15 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | + * See the License for the specific language governing permissions and |
| 18 | + * limitations under the License. |
| 19 | + */ |
| 20 | + |
| 21 | +package com.apple.foundationdb.record.query.plan.cascades.debug; |
| 22 | + |
| 23 | +import com.apple.foundationdb.record.query.plan.cascades.PlanContext; |
| 24 | +import com.apple.foundationdb.record.query.plan.cascades.PlannerPhase; |
| 25 | +import com.apple.foundationdb.record.query.plan.cascades.Quantifier; |
| 26 | +import com.apple.foundationdb.record.query.plan.cascades.Reference; |
| 27 | +import com.apple.foundationdb.record.query.plan.cascades.expressions.RelationalExpression; |
| 28 | +import com.apple.foundationdb.record.query.plan.cascades.expressions.SelectExpression; |
| 29 | +import com.apple.foundationdb.record.query.plan.cascades.values.LiteralValue; |
| 30 | +import org.jline.terminal.Terminal; |
| 31 | +import org.jline.terminal.TerminalBuilder; |
| 32 | +import org.junit.jupiter.api.BeforeEach; |
| 33 | +import org.junit.jupiter.api.Test; |
| 34 | + |
| 35 | +import java.io.ByteArrayOutputStream; |
| 36 | +import java.io.IOException; |
| 37 | +import java.io.PipedInputStream; |
| 38 | +import java.io.PipedOutputStream; |
| 39 | +import java.nio.charset.StandardCharsets; |
| 40 | +import java.util.ArrayDeque; |
| 41 | +import java.util.Collections; |
| 42 | + |
| 43 | +import static org.assertj.core.api.Assertions.assertThat; |
| 44 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 45 | + |
| 46 | + |
| 47 | +class CommandsTest { |
| 48 | + private String query; |
| 49 | + private PipedOutputStream outIn; |
| 50 | + private Terminal terminal; |
| 51 | + private ByteArrayOutputStream outputStream; |
| 52 | + private PlannerRepl debugger; |
| 53 | + |
| 54 | + @BeforeEach |
| 55 | + void setUp() throws IOException { |
| 56 | + query = "SELECT * FROM A"; |
| 57 | + PipedInputStream in = new PipedInputStream(); |
| 58 | + outIn = new PipedOutputStream(in); |
| 59 | + outputStream = new ByteArrayOutputStream(2048); |
| 60 | + terminal = TerminalBuilder.builder().streams(in, outputStream).build(); |
| 61 | + debugger = new PlannerRepl(terminal, false); |
| 62 | + |
| 63 | + Debugger.setDebugger(debugger); |
| 64 | + Debugger.setup(); |
| 65 | + Debugger.withDebugger(d -> d.onQuery(query, PlanContext.EMPTY_CONTEXT)); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + void testBreakCommand() throws IOException { |
| 70 | + outIn.write("break rule somerule BEGIN\nbreak list\ncont\n".getBytes(StandardCharsets.UTF_8)); |
| 71 | + |
| 72 | + StatsDebugger.withDebugger( |
| 73 | + d -> d.onEvent(new Debugger.InitiatePlannerPhaseEvent( |
| 74 | + PlannerPhase.REWRITING, Reference.empty(), new ArrayDeque<>(), Debugger.Location.BEGIN)) |
| 75 | + ); |
| 76 | + |
| 77 | + terminal.writer().close(); |
| 78 | + assertThat(outputStream.toString()).contains( |
| 79 | + ReplTestUtil.coloredKeyValue("id", "0"), |
| 80 | + ReplTestUtil.coloredKeyValue("kind", "OnRuleBreakPoint"), |
| 81 | + ReplTestUtil.coloredKeyValue("location", "BEGIN"), |
| 82 | + ReplTestUtil.coloredKeyValue("ruleNamePrefix", "somerule") |
| 83 | + ); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + void testCurrentCommand() throws IOException { |
| 88 | + outIn.write("current\ncont\n".getBytes(StandardCharsets.UTF_8)); |
| 89 | + |
| 90 | + StatsDebugger.withDebugger( |
| 91 | + d -> d.onEvent(new Debugger.InitiatePlannerPhaseEvent( |
| 92 | + PlannerPhase.REWRITING, Reference.empty(), new ArrayDeque<>(), Debugger.Location.BEGIN)) |
| 93 | + ); |
| 94 | + |
| 95 | + terminal.writer().close(); |
| 96 | + assertThat(outputStream.toString()).contains( |
| 97 | + ReplTestUtil.coloredKeyValue("event", "initphase"), |
| 98 | + ReplTestUtil.coloredKeyValue("description", "initiating planner phase") |
| 99 | + ); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + void testEventsCommand() throws IOException { |
| 104 | + outIn.write("step 1\nstep 1\nevents\ncont\n".getBytes(StandardCharsets.UTF_8)); |
| 105 | + final RelationalExpression exp1 = new SelectExpression( |
| 106 | + LiteralValue.ofScalar(1), Collections.emptyList(), Collections.emptyList()); |
| 107 | + |
| 108 | + StatsDebugger.withDebugger( |
| 109 | + d -> d.onEvent(new Debugger.InitiatePlannerPhaseEvent( |
| 110 | + PlannerPhase.REWRITING, Reference.empty(), new ArrayDeque<>(), Debugger.Location.BEGIN)) |
| 111 | + ); |
| 112 | + StatsDebugger.withDebugger( |
| 113 | + d -> d.onEvent(new Debugger.InitiatePlannerPhaseEvent( |
| 114 | + PlannerPhase.REWRITING, Reference.empty(), new ArrayDeque<>(), Debugger.Location.END)) |
| 115 | + ); |
| 116 | + StatsDebugger.withDebugger( |
| 117 | + d -> d.onEvent(Debugger.InsertIntoMemoEvent.newExp(exp1)) |
| 118 | + ); |
| 119 | + |
| 120 | + terminal.writer().close(); |
| 121 | + assertThat(outputStream.toString()).contains( |
| 122 | + String.join( |
| 123 | + "; ", |
| 124 | + ReplTestUtil.coloredKeyValue("tick", "0"), |
| 125 | + ReplTestUtil.coloredKeyValue("shorthand", "initphase") |
| 126 | + ), |
| 127 | + String.join( |
| 128 | + "; ", |
| 129 | + ReplTestUtil.coloredKeyValue("tick", "1"), |
| 130 | + ReplTestUtil.coloredKeyValue("shorthand", "initphase") |
| 131 | + ), |
| 132 | + String.join( |
| 133 | + "; ", |
| 134 | + ReplTestUtil.coloredKeyValue("tick", "2"), |
| 135 | + ReplTestUtil.coloredKeyValue("shorthand", "insert_into_memo") |
| 136 | + ) |
| 137 | + ); |
| 138 | + } |
| 139 | + |
| 140 | + @Test |
| 141 | + void testExpsCommand() throws IOException { |
| 142 | + outIn.write("exps\ncont\n".getBytes(StandardCharsets.UTF_8)); |
| 143 | + final RelationalExpression exp0 = new SelectExpression( |
| 144 | + LiteralValue.ofScalar(1), Collections.emptyList(), Collections.emptyList()); |
| 145 | + final RelationalExpression exp1 = new SelectExpression( |
| 146 | + LiteralValue.ofScalar(2), Collections.emptyList(), Collections.emptyList()); |
| 147 | + final Reference ref0 = Reference.initialOf(exp0, exp1); |
| 148 | + |
| 149 | + StatsDebugger.withDebugger( |
| 150 | + d -> d.onEvent(new Debugger.InitiatePlannerPhaseEvent( |
| 151 | + PlannerPhase.REWRITING, ref0, new ArrayDeque<>(), Debugger.Location.BEGIN)) |
| 152 | + ); |
| 153 | + |
| 154 | + terminal.writer().close(); |
| 155 | + assertThat(outputStream.toString()).contains( |
| 156 | + ReplTestUtil.coloredKeyValue("id", debugger.nameForObject(exp0)), |
| 157 | + ReplTestUtil.coloredKeyValue("id", debugger.nameForObject(exp0)) |
| 158 | + ); |
| 159 | + } |
| 160 | + |
| 161 | + @Test |
| 162 | + void testRefsCommand() throws IOException { |
| 163 | + outIn.write("refs\ncont\n".getBytes(StandardCharsets.UTF_8)); |
| 164 | + final RelationalExpression exp0 = new SelectExpression( |
| 165 | + LiteralValue.ofScalar(1), Collections.emptyList(), Collections.emptyList()); |
| 166 | + final RelationalExpression exp1 = new SelectExpression( |
| 167 | + LiteralValue.ofScalar(2), Collections.emptyList(), Collections.emptyList()); |
| 168 | + final Reference ref0 = Reference.initialOf(exp0, exp1); |
| 169 | + |
| 170 | + StatsDebugger.withDebugger( |
| 171 | + d -> d.onEvent(new Debugger.InitiatePlannerPhaseEvent( |
| 172 | + PlannerPhase.REWRITING, ref0, new ArrayDeque<>(), Debugger.Location.BEGIN)) |
| 173 | + ); |
| 174 | + |
| 175 | + terminal.writer().close(); |
| 176 | + assertThat(outputStream.toString()).contains( |
| 177 | + ReplTestUtil.coloredKeyValue("id", debugger.nameForObject(ref0)), |
| 178 | + ReplTestUtil.coloredKeyValue( |
| 179 | + "members", "{" + debugger.nameForObject(exp0) + ", " + debugger.nameForObject(exp1) + "}" |
| 180 | + ) |
| 181 | + ); |
| 182 | + } |
| 183 | + |
| 184 | + @Test |
| 185 | + void testQunsCommand() throws IOException { |
| 186 | + outIn.write("quns\ncont\n".getBytes(StandardCharsets.UTF_8)); |
| 187 | + final Reference ref0 = Reference.empty(); |
| 188 | + final Quantifier qun = Quantifier.forEach(ref0); |
| 189 | + |
| 190 | + StatsDebugger.withDebugger( |
| 191 | + d -> d.onEvent(new Debugger.InitiatePlannerPhaseEvent( |
| 192 | + PlannerPhase.REWRITING, ref0, new ArrayDeque<>(), Debugger.Location.BEGIN)) |
| 193 | + ); |
| 194 | + |
| 195 | + terminal.writer().close(); |
| 196 | + assertThat(outputStream.toString()).contains( |
| 197 | + ReplTestUtil.coloredKeyValue("id", debugger.nameForObject(qun)), |
| 198 | + ReplTestUtil.coloredKeyValue("ranges over", debugger.nameForObject(ref0)) |
| 199 | + ); |
| 200 | + } |
| 201 | + |
| 202 | + @Test |
| 203 | + void testRestartCommand() throws IOException { |
| 204 | + outIn.write("restart\n".getBytes(StandardCharsets.UTF_8)); |
| 205 | + final EventState eventStateBeforeRestart = debugger.getCurrentState(); |
| 206 | + final SymbolTables symbolStateBeforeRestart = debugger.getCurrentSymbolState(); |
| 207 | + |
| 208 | + assertThatThrownBy(() -> StatsDebugger.withDebugger( |
| 209 | + d -> d.onEvent(new Debugger.InitiatePlannerPhaseEvent( |
| 210 | + PlannerPhase.REWRITING, Reference.empty(), new ArrayDeque<>(), Debugger.Location.BEGIN))) |
| 211 | + ).isInstanceOf(RestartException.class); |
| 212 | + |
| 213 | + assertThat(debugger.getCurrentState()).isNotSameAs(eventStateBeforeRestart); |
| 214 | + assertThat(debugger.getCurrentSymbolState()).isNotSameAs(symbolStateBeforeRestart); |
| 215 | + } |
| 216 | +} |
0 commit comments