11/*
2- * Copyright (c) 2003, 2017 , Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2003, 2022 , Oracle and/or its affiliates. All rights reserved.
33 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44 *
55 * This code is free software; you can redistribute it and/or modify it
3131import javax .sound .midi .Sequencer ;
3232import javax .sound .midi .ShortMessage ;
3333import javax .sound .midi .Track ;
34+ import java .util .ArrayList ;
35+ import java .util .Collections ;
36+ import java .util .List ;
3437
3538/**
3639 * @test
3740 * @bug 4347135
3841 * @summary MIDI MetaMessage callback inconsistent
3942 * @key intermittent sound
40- * @run main/othervm MetaCallback
43+ * @run main/othervm/timeout=120 MetaCallback
4144 */
4245public class MetaCallback implements MetaEventListener {
4346
@@ -59,11 +62,15 @@ static ShortMessage MidiMsg3(int a, int b, int c) {
5962
6063 public static int TOTAL_COUNT = 100 ;
6164
62- int metaCount = 0 ;
63- boolean finished = false ;
65+ volatile int metaCount = 0 ;
66+ volatile boolean finished = false ;
67+ // On M1 Mac sometimes system notifies listener about the same message twice
68+ static List <MetaMessage > received = Collections .synchronizedList (new ArrayList <>());
69+ long startTimeMs ;
6470
6571 MetaCallback () throws Exception {
6672
73+ startTimeMs = System .currentTimeMillis ();
6774 sequencer =MidiSystem .getSequencer ();
6875 sequence =new Sequence (Sequence .PPQ ,240 );
6976 track =sequence .createTrack ();
@@ -101,13 +108,28 @@ static ShortMessage MidiMsg3(int a, int b, int c) {
101108 }
102109 }
103110 void start () {sequencer .start ();}
104- void stop () {sequencer .stop ();}
111+ void stop () {
112+ sequencer .stop ();
113+ sequencer .close ();
114+ }
105115
106116 public void meta (MetaMessage msg ) {
107117 System .out .println ("" +metaCount +": got " +msg );
108118 if (msg .getType () == 0x2F ) {
109119 finished = true ;
110120 } else if (msg .getData ().length > 0 && msg .getType () == 1 ) {
121+ if (!received .contains (msg )) {
122+ received .add (msg );
123+ } else {
124+ // Add some additional debug output for the case of duplicate meta message
125+ // The test will fail anyway at the end
126+ System .out .println ("Duplicate message received after getting "
127+ + received .size () + " messages." );
128+ System .out .println ("Sequencer in use: " + sequencer );
129+ System .out .println ("Time from test start: " +
130+ (System .currentTimeMillis () - startTimeMs ) + "ms" );
131+ Thread .dumpStack ();
132+ }
111133 metaCount ++;
112134 }
113135 }
0 commit comments