damondouglas commented on code in PR #29829:
URL: https://github.com/apache/beam/pull/29829#discussion_r1437867909


##########
sdks/go/test/integration/primitives/timers.go:
##########
@@ -0,0 +1,133 @@
+// 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 primitives
+
+import (
+       "context"
+       "strconv"
+
+       "github.com/apache/beam/sdks/v2/go/pkg/beam"
+       "github.com/apache/beam/sdks/v2/go/pkg/beam/core/state"
+       "github.com/apache/beam/sdks/v2/go/pkg/beam/core/timers"
+       "github.com/apache/beam/sdks/v2/go/pkg/beam/register"
+       "github.com/apache/beam/sdks/v2/go/pkg/beam/testing/passert"
+)
+
+// Based on 
https://github.com/apache/beam/blob/master/runners/flink/src/test/java/org/apache/beam/runners/flink/PortableTimersExecutionTest.java
+
+func init() {
+       register.DoFn2x0[[]byte, func(string, int)](&inputFn[string, int]{})
+       register.DoFn6x0[beam.Window, state.Provider, timers.Provider, string, 
int, func(kv[string, int])](&eventTimeFn{})
+       register.Emitter2[string, int]()
+       register.Emitter1[kv[string, int]]()
+}
+
+type kv[K, V any] struct {
+       Key   K
+       Value V
+}
+
+func kvfn[K, V any](k K, v V) kv[K, V] {
+       return kv[K, V]{k, v}
+}
+
+type inputFn[K, V any] struct {
+       Inputs []kv[K, V]
+}
+
+func (fn *inputFn[K, V]) ProcessElement(_ []byte, emit func(K, V)) {
+       for _, in := range fn.Inputs {
+               emit(in.Key, in.Value)
+       }
+}
+
+type eventTimeFn struct {
+       Callback timers.EventTime
+       MyKey    state.Value[string]
+
+       Offset      int
+       TimerOutput int
+}
+
+func (fn *eventTimeFn) ProcessElement(w beam.Window, sp state.Provider, tp 
timers.Provider, key string, value int, emit func(kv[string, int])) {
+       fn.Callback.Set(tp, w.MaxTimestamp().ToTime())
+       fn.MyKey.Write(sp, key)
+       emit(kvfn(key, value+fn.Offset))
+}
+
+func (fn *eventTimeFn) OnTimer(ctx context.Context, ts beam.EventTime, sp 
state.Provider, tp timers.Provider, key string, timer timers.Context, emit 
func(kv[string, int])) {
+       switch timer.Family {
+       case fn.Callback.Family:
+               switch timer.Tag {
+               case "":
+                       read, ok, err := fn.MyKey.Read(sp)
+                       if err != nil {
+                               panic(err)
+                       }
+                       if !ok {
+                               panic("State must be set.")
+                       }
+                       emit(kvfn(read, fn.TimerOutput))
+               }
+       default:
+               if fn.Callback.Family != timer.Family || timer.Tag != "" {
+                       panic("unexpected timer family: " + timer.Family + " 
tag:" + timer.Tag + " want: " + fn.Callback.Family)
+               }
+       }
+}
+
+// TimersEventTime takes in an impulse transform and then validates
+// event time timer execution.
+//
+// The impulse is provided outside to swap between a bounded impulse, and
+// an unbounded one, because the Go SDK uses that to determine if a pipeline
+// is "streaming" or not. This matters at least for executions on Dataflow.
+//
+// Regardless,the pipelines should pass.

Review Comment:
   I like how you re-worded the comment. It's more clear now. Feel free to 
resolve this conversation if you'd like.



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to