anishshri-db commented on code in PR #53159:
URL: https://github.com/apache/spark/pull/53159#discussion_r2656013901


##########
sql/core/src/main/scala/org/apache/spark/sql/streaming/TwsTester.scala:
##########
@@ -0,0 +1,286 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.spark.sql.streaming
+
+import java.sql.Timestamp
+import java.time.{Clock, Instant, ZoneId}
+
+import scala.reflect.ClassTag
+
+import 
org.apache.spark.sql.execution.streaming.operators.stateful.transformwithstate.statefulprocessor.ImplicitGroupingKeyTracker
+import 
org.apache.spark.sql.execution.streaming.operators.stateful.transformwithstate.testing.InMemoryStatefulProcessorHandle
+import 
org.apache.spark.sql.execution.streaming.operators.stateful.transformwithstate.timers.{ExpiredTimerInfoImpl,
 TimerValuesImpl}
+
+/**
+ * Testing utility for transformWithState stateful processors.
+ *
+ * This class enables unit testing of StatefulProcessor business logic by 
simulating the
+ * behavior of transformWithState. It processes input rows and returns output 
rows equivalent
+ * to those that would be produced by the processor in an actual Spark 
streaming query.
+ *
+ * '''Supported:'''
+ *  - Processing input rows and producing output rows via `test()`.
+ *  - Initial state setup via constructor parameter.
+ *  - Direct state manipulation via `setValueState`, `setListState`, 
`setMapState`.
+ *  - Direct state inspection via `peekValueState`, `peekListState`, 
`peekMapState`.
+ *  - Timers in ProcessingTime mode (use `advanceProcessingTime` to fire 
timers).
+ *  - Timers in EventTime mode (use `eventTimeExtractor` and 
`watermarkDelayMs` to configure;
+ *    watermark advances automatically based on event times, or use 
`advanceWatermark` manually).
+ *  - Late event filtering in EventTime mode (events older than the current 
watermark are dropped).
+ *  - TTL for ValueState, ListState, and MapState (use ProcessingTime mode and
+ *    `advanceProcessingTime` to test expiry).
+ *
+ * '''Testing EventTime Mode:'''
+ *  To test with EventTime, provide `eventTimeExtractor` (a function 
extracting the event
+ *  timestamp from each input row) and `watermarkDelayMs` (the watermark delay 
in milliseconds).
+ *  The watermark is computed as `max(event_time_seen) - watermarkDelayMs` and 
is updated
+ *  automatically after each `test()` call. Late events (with event time older 
than the current
+ *  watermark) are filtered out before processing, matching production 
behavior. Timers with
+ *  expiry time <= watermark will fire. You can also manually advance the 
watermark using
+ *  `advanceWatermark()`.
+ *
+ * '''Use Cases:'''
+ *  - '''Primary''': Unit testing business logic in `handleInputRows` 
implementations.
+ *  - '''Not recommended''': End-to-end testing or performance testing - use 
actual Spark

Review Comment:
   Can we also add docs in the programming guide ? doesn't have to be this PR - 
can create a separate JIRA and PR for the same



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to