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


##########
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 {

Review Comment:
   In this test? No.
   
   But if there are *multiple* timers, the same OnTimer callback is used, so 
this is to differentiate them. This is just the recommended pattern for timers. 
Usually I'm all about making the code shorter, but it's better to establish a 
pattern on how to use timers, in this case, using a switch.
   
   If the *runner* screws up, the family or the tag could be wrong. Since this 
pipeline is partly to validate runner behavior, we need to check it is what we 
expect, and provide some meaningful error if it's wrong (at least the timer 
bits).



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