4444import org .apache .kafka .connect .storage .OffsetStorageReader ;
4545import org .junit .After ;
4646import org .junit .Before ;
47+ import org .junit .Rule ;
4748import org .junit .Test ;
49+ import org .junit .rules .ExpectedException ;
4850
4951import static org .junit .Assert .assertEquals ;
5052import static org .junit .Assert .assertNotEquals ;
@@ -61,6 +63,9 @@ public class DatagenTaskTest {
6163
6264 private static final AvroData AVRO_DATA = new AvroData (20 );
6365
66+ @ Rule
67+ public ExpectedException expectedException = ExpectedException .none ();
68+
6469 private Map <String , String > config ;
6570 private DatagenTask task ;
6671 private List <SourceRecord > records ;
@@ -223,6 +228,36 @@ public void shouldFailToGenerateMoreRecordsThanSpecified() throws Exception {
223228 }
224229 }
225230
231+ /**
232+ * <p>
233+ * This test is non-deterministic. This test might fail in case avro-random-generator library
234+ * updates/changes its underlying implementation.
235+ * </p>
236+ * @throws Exception
237+ */
238+ @ Test
239+ public void shouldFailToGenerateComplexRegexWithTimeout () throws Exception {
240+ createTaskWithSchemaAndTimeout ("regex_schema_timeout.avro" , "regex_key" , "100" );
241+ expectedException .expect (ConnectException .class );
242+ expectedException .expectMessage ("Record generation timed out" );
243+ generateRecords ();
244+ }
245+
246+ /**
247+ * <p>
248+ * This test is non-deterministic. This test might fail in case avro-random-generator library
249+ * updates/changes its underlying implementation.
250+ * </p>
251+ * @throws Exception
252+ */
253+ @ Test
254+ public void shouldFailToGenerateComplexRegexWithStackOverflow () throws Exception {
255+ createTaskWithSchemaAndTimeout ("regex_schema_stackoverflow.avro" , "regex_key" , "100" );
256+ expectedException .expect (ConnectException .class );
257+ expectedException .expectMessage ("Unable to generate random record" );
258+ generateRecords ();
259+ }
260+
226261 private void generateAndValidateRecordsFor (DatagenTask .Quickstart quickstart ) throws Exception {
227262 createTaskWith (quickstart );
228263 generateRecords ();
@@ -337,6 +372,17 @@ private void createTaskWithSchema(String schemaResourceFilename, String idFieldN
337372 loadKeyAndValueSchemas (schemaResourceFilename , idFieldName );
338373 }
339374
375+ private void createTaskWithSchemaAndTimeout (String schemaResourceFile , String idFieldName ,
376+ String timeout ) {
377+ dropSchemaSourceConfigs ();
378+ config .put (DatagenConnectorConfig .SCHEMA_FILENAME_CONF , schemaResourceFile );
379+ config .put (DatagenConnectorConfig .SCHEMA_KEYFIELD_CONF , idFieldName );
380+ config .put (DatagenConnectorConfig .GENERATE_TIMEOUT_CONF , timeout );
381+ createTask ();
382+ loadKeyAndValueSchemas (schemaResourceFile , idFieldName );
383+
384+ }
385+
340386 private void createTaskWithSchemaText (String schemaText , String keyField ) {
341387 dropSchemaSourceConfigs ();
342388 config .put (DatagenConnectorConfig .SCHEMA_STRING_CONF , schemaText );
0 commit comments