{"record":{"id":"f6314eec07120f0f","repo":"QuantConnect/Lean","slug":"expected-events-didn-t-happen","errorCode":null,"errorMessage":"Expected events didn't happen","messagePattern":"Expected events didn't happen","errorType":"exception","errorClass":"RegressionTestException","httpStatus":null,"severity":"error","filePath":"Algorithm.CSharp/CustomUniverseImmediateSelectionRegressionAlgorithm.cs","lineNumber":109,"sourceCode":"                {\n                    throw new RegressionTestException($\"Expected {ExpectedSymbols.Count} stocks to be added to the algorithm, \" +\n                        $\"but found {changes.AddedSecurities.Count}\");\n                }\n\n                if (!ExpectedSymbols.All(x => changes.AddedSecurities.Any(security => security.Symbol == x)))\n                {\n                    throw new RegressionTestException(\"Expected symbols were not added to the algorithm\");\n                }\n\n                _securitiesChanged = true;\n            }\n        }\n\n        public override void OnEndOfAlgorithm()\n        {\n            if (_firstOnData || !_selected || !_securitiesChanged)\n            {\n                throw new RegressionTestException(\"Expected events didn't happen\");\n            }\n        }\n\n        /// <summary>\n        /// This is used by the regression test system to indicate if the open source Lean repository has the required data to run this algorithm.\n        /// </summary>\n        public bool CanRunLocally { get; } = true;\n\n        /// <summary>\n        /// This is used by the regression test system to indicate which languages this algorithm is written in.\n        /// </summary>\n        public List<Language> Languages { get; } = new() { Language.CSharp };\n\n        /// <summary>\n        /// Data Points count of all timeslices of algorithm\n        /// </summary>\n        public long DataPoints => 52;\n","sourceCodeStart":91,"sourceCodeEnd":127,"githubUrl":"https://github.com/QuantConnect/Lean/blob/d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892/Algorithm.CSharp/CustomUniverseImmediateSelectionRegressionAlgorithm.cs#L91-L127","documentation":"This RegressionTestException is thrown in OnEndOfAlgorithm as a final sanity gate. It checks three boolean flags: _firstOnData (must be false, meaning OnData fired at least once), _selected (universe selector function was invoked), and _securitiesChanged (OnSecuritiesChanged fired). LEAN uses it to guarantee the full event lifecycle of the algorithm executed.","triggerScenarios":"One or more of the lifecycle events never fired: OnData was never called (no data fed to algorithm), the universe selector function was never invoked (_selected stays false), or OnSecuritiesChanged was never called. Any single missing event trips the combined condition.","commonSituations":"Data file missing or unreadable so no slices reach OnData, universe-selection disabled or broken in engine changes so the selector is never called, or SecurityChanges events suppressed by an engine refactor. Also happens when algorithm start/end dates have no market data.","solutions":["Check each flag individually to isolate which event failed: log _firstOnData, _selected, _securitiesChanged.","If _selected is false, trace the universe-selection scheduling to confirm the selector is invoked at algorithm start.","If _firstOnData is true (OnData never ran), verify data files exist for the date range and the subscription is properly created.","If _securitiesChanged is false, ensure the engine emits SecurityChanges even when the added set is non-empty on the first selection."],"exampleFix":"// before — single combined assertion gives no clue which event failed\nif (_firstOnData || !_selected || !_securitiesChanged)\n{\n    throw new RegressionTestException(\"Expected events didn't happen\");\n}\n\n// after — split for faster diagnosis\nif (_firstOnData) throw new RegressionTestException(\"OnData was never called\");\nif (!_selected) throw new RegressionTestException(\"Universe selector was never invoked\");\nif (!_securitiesChanged) throw new RegressionTestException(\"OnSecuritiesChanged was never called\");","handlingStrategy":"validation","validationCode":"// In OnEndOfAlgorithm, check each flag independently for clearer diagnosis\npublic override void OnEndOfAlgorithm()\n{\n    if (_firstOnData) Log(\"WARNING: OnData was never called\");\n    if (!_selected) Log(\"WARNING: Universe selector was never invoked\");\n    if (!_securitiesChanged) Log(\"WARNING: OnSecuritiesChanged was never called\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Set each lifecycle flag as early as possible (at the top of the handler) to catch partial-execution failures.","Split combined assertions into individual checks for faster root-cause identification.","Add logging in each handler entry point to confirm the event pipeline is firing."],"tags":["quantconnect","lean","lifecycle","regression-test","ondata","universe-selection"],"backgroundTag":null,"analyzedSha":"d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892","analyzedAt":"2026-08-13T13:52:21.013Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}