youngoli commented on a change in pull request #11327: [BEAM-9642] Add SDF 
execution units.
URL: https://github.com/apache/beam/pull/11327#discussion_r407829916
 
 

 ##########
 File path: sdks/go/pkg/beam/core/runtime/exec/sdf_test.go
 ##########
 @@ -0,0 +1,408 @@
+// 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 exec
+
+import (
+       "context"
+       "github.com/apache/beam/sdks/go/pkg/beam/core/graph"
+       "github.com/apache/beam/sdks/go/pkg/beam/core/graph/window"
+       "github.com/apache/beam/sdks/go/pkg/beam/core/typex"
+       "github.com/google/go-cmp/cmp"
+       "testing"
+)
+
+// testTimestamp is a constant used to check that timestamps are retained.
+const testTimestamp = 15
+
+// testWindow is a constant used to check that windows are retained
+var testWindows = []typex.Window{window.IntervalWindow{Start: 10, End: 20}}
+
+// TestSdfNodes verifies that the various SDF nodes fulfill each of their
+// described contracts, that they each successfully invoke any SDF methods
+// needed, and that they preserve timestamps and windows correctly.
+func TestSdfNodes(t *testing.T) {
+       // Setup. The DoFns created below are defined in sdf_invokers_test.go 
and
+       // have testable behavior to confirm that they got correctly invoked.
+       // Without knowing the expected behavior of these DoFns, the desired 
outputs
+       // in the unit tests below will not make much sense.
+       dfn, err := graph.NewDoFn(&Sdf{}, graph.NumMainInputs(graph.MainSingle))
+       if err != nil {
+               t.Fatalf("invalid function: %v", err)
+       }
+       kvdfn, err := graph.NewDoFn(&KvSdf{}, graph.NumMainInputs(graph.MainKv))
+       if err != nil {
+               t.Fatalf("invalid function: %v", err)
+       }
+
+       // Validate PairWithRestriction matches its contract and properly 
invokes
+       // SDF method CreateInitialRestriction.
+       t.Run("PairWithRestriction", func(t *testing.T) {
+               tests := []struct {
+                       name string
+                       fn   *graph.DoFn
+                       in   *FullValue
+                       want *FullValue
+               }{
+                       {
+                               name: "SingleElem",
+                               fn:   dfn,
+                               in: &FullValue{
+                                       Elm:       5,
+                                       Elm2:      nil,
+                                       Timestamp: testTimestamp,
+                                       Windows:   testWindows,
+                               },
+                               want: &FullValue{
+                                       Elm: &FullValue{
+                                               Elm:       5,
+                                               Elm2:      nil,
+                                               Timestamp: testTimestamp,
+                                               Windows:   testWindows,
+                                       },
+                                       Elm2:      Restriction{5},
+                                       Timestamp: testTimestamp,
+                                       Windows:   testWindows,
+                               },
+                       },
+                       {
+                               name: "KvElem",
+                               fn:   kvdfn,
+                               in: &FullValue{
+                                       Elm:       5,
+                                       Elm2:      2,
+                                       Timestamp: testTimestamp,
+                                       Windows:   testWindows,
+                               },
+                               want: &FullValue{
+                                       Elm: &FullValue{
+                                               Elm:       5,
+                                               Elm2:      2,
+                                               Timestamp: testTimestamp,
+                                               Windows:   testWindows,
+                                       },
+                                       Elm2:      Restriction{7},
+                                       Timestamp: testTimestamp,
+                                       Windows:   testWindows,
+                               },
+                       },
+               }
+               for _, test := range tests {
+                       test := test
+                       t.Run(test.name, func(t *testing.T) {
+                               ctx := context.Background()
+                               fake := &FakeNode{}
+                               node := PairWithRestriction{UID: 0, Fn: 
test.fn, Out: []Node{fake}}
+
+                               if err := node.Up(ctx); err != nil {
+                                       t.Fatalf("Up failed: %v", err)
+                               }
+                               if err := node.StartBundle(ctx, "bundle_id", 
DataContext{}); err != nil {
+                                       t.Fatalf("StartBundle failed: %v", err)
+                               }
+
+                               if err := node.ProcessElement(ctx, test.in); 
err != nil {
+                                       t.Fatalf("ProcessElement failed: %v", 
err)
+                               }
+                               got := fake.Vals[0]
+                               if !cmp.Equal(got, test.want) {
 
 Review comment:
   Oooh, that seems like a good idea. Made a bug for it: 
https://jira.apache.org/jira/browse/BEAM-9753

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to