You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by GitBox <gi...@apache.org> on 2020/04/09 18:49:55 UTC

[GitHub] [beam] lostluck commented on a change in pull request #11327: [BEAM-9642] Add SDF execution units.

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

 ##########
 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) {
+					t.Errorf("ProcessElement(%v) has incorrect output: got: %v, want: %v",
+						test.in, got, test.want)
+				}
+				if err := node.FinishBundle(ctx); err != nil {
+					t.Fatalf("FinishBundle failed: %v", err)
+				}
+				if err := node.Down(ctx); err != nil {
+					t.Fatalf("Down failed: %v", err)
+				}
+			})
+		}
+	})
+
+	// Validate SplitAndSizeRestrictions matches its contract and properly
+	// invokes SDF methods SplitRestriction and RestrictionSize.
+	t.Run("SplitAndSizeRestrictions", func(t *testing.T) {
+		tests := []struct {
+			name string
+			fn   *graph.DoFn
+			in   *FullValue
+			want []*FullValue
+		}{
+			{
+				name: "SingleElem",
+				fn:   dfn,
+				in: &FullValue{
+					Elm: &FullValue{
+						Elm:       2,
+						Elm2:      nil,
+						Timestamp: testTimestamp,
+						Windows:   testWindows,
+					},
+					Elm2:      Restriction{5},
+					Timestamp: testTimestamp,
+					Windows:   testWindows,
+				},
+				want: []*FullValue{
+					{
+						Elm: &FullValue{
+							Elm: &FullValue{
+								Elm:       2,
+								Elm2:      nil,
+								Timestamp: testTimestamp,
+								Windows:   testWindows,
+							},
+							Elm2: Restriction{7},
+						},
+						Elm2:      9.0,
+						Timestamp: testTimestamp,
+						Windows:   testWindows,
+					},
+					{
+						Elm: &FullValue{
+							Elm: &FullValue{
+								Elm:       2,
+								Elm2:      nil,
+								Timestamp: testTimestamp,
+								Windows:   testWindows,
+							},
+							Elm2: Restriction{8},
+						},
+						Elm2:      10.0,
+						Timestamp: testTimestamp,
+						Windows:   testWindows,
+					},
+				},
+			},
+			{
+				name: "KvElem",
+				fn:   kvdfn,
+				in: &FullValue{
+					Elm: &FullValue{
+						Elm:       2,
+						Elm2:      5,
+						Timestamp: testTimestamp,
+						Windows:   testWindows,
+					},
+					Elm2:      Restriction{3},
+					Timestamp: testTimestamp,
+					Windows:   testWindows,
+				},
+				want: []*FullValue{
+					{
+						Elm: &FullValue{
+							Elm: &FullValue{
+								Elm:       2,
+								Elm2:      5,
+								Timestamp: testTimestamp,
+								Windows:   testWindows,
+							},
+							Elm2: Restriction{5},
+						},
+						Elm2:      12.0,
+						Timestamp: testTimestamp,
+						Windows:   testWindows,
+					},
+					{
+						Elm: &FullValue{
+							Elm: &FullValue{
+								Elm:       2,
+								Elm2:      5,
+								Timestamp: testTimestamp,
+								Windows:   testWindows,
+							},
+							Elm2: Restriction{8},
+						},
+						Elm2:      15.0,
+						Timestamp: testTimestamp,
+						Windows:   testWindows,
+					},
+				},
+			},
+		}
+		for _, test := range tests {
+			test := test
+			t.Run(test.name, func(t *testing.T) {
+				ctx := context.Background()
+				fake := &FakeNode{}
+				node := SplitAndSizeRestrictions{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)
+				}
+				for i, got := range fake.Vals {
+					if !cmp.Equal(got, test.want[i]) {
+						t.Errorf("ProcessElement(%v) has incorrect output %v: got: %v, want: %v",
+							test.in, i, got, test.want)
+					}
+				}
+				if err := node.FinishBundle(ctx); err != nil {
+					t.Fatalf("FinishBundle failed: %v", err)
+				}
+				if err := node.Down(ctx); err != nil {
+					t.Fatalf("Down failed: %v", err)
+				}
+			})
+		}
+	})
+
+	// Validate ProcessSizedElementsAndRestrictions matches its contract and
+	// properly invokes SDF methods CreateTracker and ProcessElement.
+	t.Run("ProcessSizedElementsAndRestrictions", func(t *testing.T) {
+		tests := []struct {
+			name string
+			fn   *graph.DoFn
+			in   *FullValue
+			want *FullValue
+		}{
+			{
+				name: "SingleElem",
+				fn:   dfn,
+				in: &FullValue{
+					Elm: &FullValue{
+						Elm: &FullValue{
+							Elm:       3,
+							Elm2:      nil,
+							Timestamp: testTimestamp,
+							Windows:   testWindows,
+						},
+						Elm2: Restriction{5},
+					},
+					Elm2:      8.0,
+					Timestamp: testTimestamp,
+					Windows:   testWindows,
+				},
+				want: &FullValue{
+					Elm:       8,
+					Elm2:      4,
+					Timestamp: testTimestamp,
+					Windows:   testWindows,
+				},
+			},
+			{
+				name: "KvElem",
+				fn:   kvdfn,
+				in: &FullValue{
+					Elm: &FullValue{
+						Elm: &FullValue{
+							Elm:       3,
+							Elm2:      10,
+							Timestamp: testTimestamp,
+							Windows:   testWindows,
+						},
+						Elm2: Restriction{5},
+					},
+					Elm2:      18.0,
+					Timestamp: testTimestamp,
+					Windows:   testWindows,
+				},
+				want: &FullValue{
+					Elm:       8,
+					Elm2:      12,
+					Timestamp: testTimestamp,
+					Windows:   testWindows,
+				},
+			},
+		}
+		for _, test := range tests {
+			test := test
+			t.Run(test.name, func(t *testing.T) {
+				ctx := context.Background()
+				fake := &FakeNode{}
+				n := &ParDo{UID: 0, Fn: test.fn, Out: []Node{fake}}
+				node := ProcessSizedElementsAndRestrictions{PDo: n}
+
+				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 {
 
 Review comment:
   There's a lot of duplicated code for these tests. Consider using FixedRoot, and the constructAndExecutePlan helper to reduce the duplication. 
   We may want to move the constructAndExecutePlan helper to unit_test.go (in another PR).

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services