@@ -1477,10 +1477,8 @@ func (s *nodeSuite) TestValidateAccess() {
14771477}
14781478
14791479func (s * nodeSuite ) TestGetComponent () {
1480- root , err := s .newTestTree (testComponentSerializedNodes ())
1481- s .NoError (err )
1482-
14831480 errValidation := errors .New ("some random validation error" )
1481+
14841482 expectedTestComponent := & TestComponent {}
14851483 setTestComponentFields (expectedTestComponent , s .nodeBackend )
14861484 assertTestComponent := func (component Component ) {
@@ -1494,39 +1492,47 @@ func (s *nodeSuite) TestGetComponent() {
14941492
14951493 testCases := []struct {
14961494 name string
1497- chasmContext Context
1495+ chasmContextFn func ( root * Node ) Context
14981496 ref ComponentRef
14991497 expectedErr error
1500- valueState valueState
1498+ nodeDirty bool
15011499 assertComponent func (Component )
15021500 }{
15031501 {
1504- name : "path not found" ,
1505- chasmContext : NewContext (context .Background (), root ),
1502+ name : "path not found" ,
1503+ chasmContextFn : func (root * Node ) Context {
1504+ return NewContext (context .Background (), root )
1505+ },
15061506 ref : ComponentRef {
15071507 componentPath : []string {"unknownComponent" },
15081508 },
15091509 expectedErr : errComponentNotFound ,
15101510 },
15111511 {
1512- name : "archetype mismatch" ,
1513- chasmContext : NewContext (context .Background (), root ),
1512+ name : "archetype mismatch" ,
1513+ chasmContextFn : func (root * Node ) Context {
1514+ return NewContext (context .Background (), root )
1515+ },
15141516 ref : ComponentRef {
15151517 archetype : "TestLibrary.test_sub_component_1" ,
15161518 },
15171519 expectedErr : errComponentNotFound ,
15181520 },
15191521 {
1520- name : "entityGoType mismatch" ,
1521- chasmContext : NewContext (context .Background (), root ),
1522+ name : "entityGoType mismatch" ,
1523+ chasmContextFn : func (root * Node ) Context {
1524+ return NewContext (context .Background (), root )
1525+ },
15221526 ref : ComponentRef {
15231527 entityGoType : reflect .TypeFor [* TestSubComponent2 ](),
15241528 },
15251529 expectedErr : errComponentNotFound ,
15261530 },
15271531 {
1528- name : "initialVT mismatch" ,
1529- chasmContext : NewContext (context .Background (), root ),
1532+ name : "initialVT mismatch" ,
1533+ chasmContextFn : func (root * Node ) Context {
1534+ return NewMutableContext (context .Background (), root )
1535+ },
15301536 ref : ComponentRef {
15311537 componentPath : []string {"SubComponent1" , "SubComponent11" },
15321538 // should be (1, 1) but we set it to (2, 2)
@@ -1538,8 +1544,10 @@ func (s *nodeSuite) TestGetComponent() {
15381544 expectedErr : errComponentNotFound ,
15391545 },
15401546 {
1541- name : "validation failure" ,
1542- chasmContext : NewContext (context .Background (), root ),
1547+ name : "validation failure" ,
1548+ chasmContextFn : func (root * Node ) Context {
1549+ return NewMutableContext (context .Background (), root )
1550+ },
15431551 ref : ComponentRef {
15441552 componentPath : []string {"SubComponent1" },
15451553 componentInitialVT : & persistencespb.VersionedTransition {
@@ -1553,8 +1561,10 @@ func (s *nodeSuite) TestGetComponent() {
15531561 expectedErr : errValidation ,
15541562 },
15551563 {
1556- name : "success readonly access" ,
1557- chasmContext : NewContext (context .Background (), root ),
1564+ name : "success readonly access" ,
1565+ chasmContextFn : func (root * Node ) Context {
1566+ return NewContext (context .Background (), root )
1567+ },
15581568 ref : ComponentRef {
15591569 componentPath : []string {}, // root
15601570 componentInitialVT : & persistencespb.VersionedTransition {
@@ -1566,32 +1576,42 @@ func (s *nodeSuite) TestGetComponent() {
15661576 },
15671577 },
15681578 expectedErr : nil ,
1569- valueState : valueStateSynced ,
15701579 assertComponent : assertTestComponent ,
15711580 },
15721581 {
1573- name : "success mutable access" ,
1574- chasmContext : NewMutableContext (context .Background (), root ),
1582+ name : "success mutable access" ,
1583+ chasmContextFn : func (root * Node ) Context {
1584+ return NewMutableContext (context .Background (), root )
1585+ },
15751586 ref : ComponentRef {
15761587 componentPath : []string {}, // root
15771588 },
15781589 expectedErr : nil ,
1579- valueState : valueStateNeedSyncStructure ,
1590+ nodeDirty : true ,
15801591 assertComponent : assertTestComponent ,
15811592 },
15821593 }
15831594
15841595 for _ , tc := range testCases {
15851596 s .Run (tc .name , func () {
1586- component , err := root .Component (tc .chasmContext , tc .ref )
1597+ root , err := s .newTestTree (testComponentSerializedNodes ())
1598+ s .NoError (err )
1599+
1600+ component , err := root .Component (tc .chasmContextFn (root ), tc .ref )
15871601 s .Equal (tc .expectedErr , err )
1588- if tc .expectedErr == nil {
1589- // s.Equal(tc.expectedComponent, component)
15901602
1591- node , ok := root .findNode (tc .ref .componentPath )
1603+ node , ok := root .findNode (tc .ref .componentPath )
1604+ if tc .expectedErr == nil {
15921605 s .True (ok )
1593- s .Equal (component , node .value )
1594- s .Equal (tc .valueState , node .valueState )
1606+ tc .assertComponent (component )
1607+ }
1608+
1609+ if ok {
1610+ if tc .nodeDirty {
1611+ s .Greater (node .valueState , valueStateSynced )
1612+ } else {
1613+ s .LessOrEqual (node .valueState , valueStateSynced )
1614+ }
15951615 }
15961616 })
15971617 }
@@ -2739,30 +2759,38 @@ func (s *nodeSuite) TestExecutePureTask() {
27392759 }
27402760
27412761 // Succeed task execution and validation (happy case).
2762+ root .setValueState (valueStateSynced )
27422763 expectExecute (nil )
27432764 expectValidate (true , nil )
27442765 executed , err := root .ExecutePureTask (ctx , taskAttributes , pureTask )
27452766 s .NoError (err )
27462767 s .True (executed )
2768+ s .Equal (valueStateNeedSyncStructure , root .valueState )
27472769
27482770 expectedErr := errors .New ("dummy" )
27492771
27502772 // Succeed validation, fail execution.
2773+ root .setValueState (valueStateSynced )
27512774 expectExecute (expectedErr )
27522775 expectValidate (true , nil )
27532776 _ , err = root .ExecutePureTask (ctx , taskAttributes , pureTask )
27542777 s .ErrorIs (expectedErr , err )
2778+ s .Equal (valueStateNeedSyncStructure , root .valueState )
27552779
27562780 // Fail task validation (no execution occurs).
2781+ root .setValueState (valueStateSynced )
27572782 expectValidate (false , nil )
27582783 executed , err = root .ExecutePureTask (ctx , taskAttributes , pureTask )
27592784 s .NoError (err )
27602785 s .False (executed )
2786+ s .Equal (valueStateSynced , root .valueState ) // task not executed, so node is clean
27612787
27622788 // Error during task validation (no execution occurs).
2789+ root .setValueState (valueStateSynced )
27632790 expectValidate (false , expectedErr )
27642791 _ , err = root .ExecutePureTask (ctx , taskAttributes , pureTask )
27652792 s .ErrorIs (expectedErr , err )
2793+ s .Equal (valueStateSynced , root .valueState ) // task not executed, so node is clean
27662794}
27672795
27682796func (s * nodeSuite ) TestExecuteSideEffectTask () {
0 commit comments